Exception error thrown when trying to read a file

G

Guest

I am trying to read a file in with the following code and I am getting a system.nullreferenceexception error. The error is “object reference not set to an instance of an objectâ€. This is my code below and I can’t figure out what I am doing wrong. I have used this before and know it works….there is no reason for this not too. Help, please

Private strPhraseFile As Strin
Private intPhrases As Intege
Private objReader As StreamReade

Private Sub mnuGamePhrase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuGamePhrase.Clic
Dim intX As Intege
Dim strDummy As Strin

Me.dlgOpen.Filter = "Text files {*.txt}| *.txt|All files {*.*}|*.*
Me.dlgOpen.InitialDirectory = Application.StartupPat
Me.dlgOpen.FilterIndex =
Me.dlgOpen.Title = "Open Phrases file

If Me.dlgOpen.ShowDialog = DialogResult.OK Then 'display dialog and accept user inpu
strPhraseFile = Me.dlgOpen.FileNam
Dim objReader As StreamReader = New StreamReader(strPhraseFile
End I

intPhrases =
strDummy = "Dummy Value
Do Until strDummy = Nothing 'Count the record
strDummy = objReader.ReadLine(
If strDummy = Nothing The
Els
intPhrases = intPhrases +
End I
Loo

objReader.Close(
objReader = Nothin


End Su
 
H

Herfried K. Wagner [MVP]

* =?Utf-8?B?a3ltamF5?= said:
I am trying to read a file in with the following code and I am getting
a system.nullreferenceexception error. The error is “object reference
not set to an instance of an object�. This is my code below and I
can’t figure out what I am doing wrong. I have used this before and
know it works….there is no reason for this not too. Help, please.

Which line throws the exception?
 
D

Dan Forster

kymjay,

You have declared objReader as StreamReader at the module level (which is
fine)

But... When you instantiate within the If block you use:

Dim objReader As StreamReader = New StreamReader(strPhraseFile)

This creates a separate variable objReader and does not set the other
objReader to be the new StreamReader. When the If block ends, the local
objReader goes out of scope so when you try to access the objReader you are
accessing the module level variable which is at its default value 'Nothing'
hence the error.

In order to rectify this change the line within the If block to read:
objReader = New StreamReader(strPhraseFile)

Regards,

Dan



kymjay said:
I am trying to read a file in with the following code and I am getting a
system.nullreferenceexception error. The error is "object reference not set
to an instance of an object". This is my code below and I can't figure out
what I am doing wrong. I have used this before and know it works..there is
no reason for this not too. Help, please.
Private strPhraseFile As String
Private intPhrases As Integer
Private objReader As StreamReader

Private Sub mnuGamePhrase_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuGamePhrase.Click
 
D

Dan Forster

I have already replied to this once but it wasn't posted on the thread!

All you have to do is change the line in you If Block so that you assign
rather than declare:

If Me.dlgOpen.ShowDialog = DialogResult.OK Then 'display dialog and
accept user input
strPhraseFile = Me.dlgOpen.FileName
objReader = New StreamReader(strPhraseFile)
End If

This will ensure that your module level variable holds the reference and not
a local objReader variable local to your If block.

Regards,

Dan

kymjay said:
I am trying to read a file in with the following code and I am getting a
system.nullreferenceexception error. The error is "object reference not set
to an instance of an object". This is my code below and I can't figure out
what I am doing wrong. I have used this before and know it works..there is
no reason for this not too. Help, please.
Private strPhraseFile As String
Private intPhrases As Integer
Private objReader As StreamReader

Private Sub mnuGamePhrase_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuGamePhrase.Click
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top