473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Exception error thrown when trying to read a file

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

Nov 20 '05 #1
3 1779
* =?Utf-8?B?a3ltamF5?= <an*******@discussions.microsoft.com> scripsit:
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?

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2
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" <an*******@discussions.microsoft.com> wrote in message
news:60**********************************@microsof t.com...
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 Dim intX As Integer
Dim strDummy As String

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

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

intPhrases = 0
strDummy = "Dummy Value"
Do Until strDummy = Nothing 'Count the records strDummy = objReader.ReadLine()
If strDummy = Nothing Then
Else
intPhrases = intPhrases + 1
End If
Loop

objReader.Close()
objReader = Nothing

End Sub

Nov 20 '05 #3
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" <an*******@discussions.microsoft.com> wrote in message
news:60**********************************@microsof t.com... 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 Dim intX As Integer
Dim strDummy As String

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

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

intPhrases = 0
strDummy = "Dummy Value"
Do Until strDummy = Nothing 'Count the records strDummy = objReader.ReadLine()
If strDummy = Nothing Then
Else
intPhrases = intPhrases + 1
End If
Loop

objReader.Close()
objReader = Nothing

End Sub

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Herring | last post by:
Hi there: Well, let me start off by saying that I am a Visual Studio drag and drop weenie, not a real programmer. So I tend to get confused when things do not look like the instructions said they...
7
by: Ekim | last post by:
hy, I've a question concerning exception-handling in c++: is there a possibility to catch any exception (I know one can do that by "catch(...)") and display its default-error-message (like in...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
7
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
19
by: Diego F. | last post by:
I think I'll never come across that error. It happens when running code from a DLL that tries to write to disk. I added permissions in the project folder, the wwwroot and in IIS to NETWORK_SERVICE...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
14
by: Rex | last post by:
Re: Looking for Tips/Writeup on overall approach to Exception Processing Hi All - I am fairly new to C# and am wondering how to best implement (overall) Exception Processing within my...
35
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly,...
16
by: john6630 | last post by:
Coming from the .Net world, I am used to the try...catch...finally approach to error handling. And PHP 5 now supports this approach. But I am not clear what happens to unhandled errors/exceptioins?...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.