locked
Can't fix error message: Object reference not set to an instance of an object.

    Question

  • Code Snippet

    Imports System.IO

    Public Class Form1

    Private fs As New FileStream(OpenFileDialog1.Filename, FileMode.Open)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    OpenFileDialog1.Title = "Open BMS File"

    OpenFileDialog1.Filter = "DF2 BMS Files (*.BMS)|*.BMS"

    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

    Messagebox.Show("Load Successful")

    End If

    End Sub

     

     

    Basically i'm trying to make the stream global so that I can write to it in many code blocks.  I haven't added the code to write to it yet since I can't even get it created correctly without an error message.

     

    I know the problem pertains to the highlighted code, using the "Dim" keyword gives the same message... any help?

     

     

    Wednesday, September 24, 2008 11:09 PM

Answers

  • At the time of declaring your filestream the OpenFileDialog hasn't been created, hence your error.  Try this instead:

     

    Friend fs As FileStream

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    OpenFileDialog1.Title = "Open BMS File"

    OpenFileDialog1.Filter = "DF2 BMS Files (*.BMS)|*.BMS"

    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

    fs = New FileStream(OpenFileDialog1.FileName, FileMode.Open)

    MessageBox.Show("Load Successful")

    End If

    End Sub

     

     

     

    Thursday, September 25, 2008 4:06 PM

All replies

  • You need to define fs as Public if you want to access it from other code modules. Since it is defined in a class (Form1) you will also need to fully qualify the reference by including the class name.

     

    In addition, I don't see where OpenFileDialog1 has been defined and created so that could cause an error in your declare.

     

    Thursday, September 25, 2008 1:13 PM
  • Ok, i'll try the keyword Public thanks, but can you elaborate a bit on qualifying the reference by including the class name?

     

    OpenFileDialog1 is a control I added to the form via tool-box, it wasn't declared in code, figured I would use it this way in order to have the filename available in all code procedures. 

    Thursday, September 25, 2008 1:42 PM
  • Form1.fs would be how you would fully qualify the reference. The fs object should show up via Intellisense once you enter Form1. Also, you may want to give fs a more meaningful name.

     

    Thursday, September 25, 2008 2:10 PM
  • At the time of declaring your filestream the OpenFileDialog hasn't been created, hence your error.  Try this instead:

     

    Friend fs As FileStream

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    OpenFileDialog1.Title = "Open BMS File"

    OpenFileDialog1.Filter = "DF2 BMS Files (*.BMS)|*.BMS"

    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

    fs = New FileStream(OpenFileDialog1.FileName, FileMode.Open)

    MessageBox.Show("Load Successful")

    End If

    End Sub

     

     

     

    Thursday, September 25, 2008 4:06 PM
  • Thanks!!!!! Much better!

    Friday, September 26, 2008 12:22 PM