Private Sub TextFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextFileToolStripMenuItem.Click
fd.Filter = "Text Files (*.txt)|*.txt"
If fd.ShowDialog() = DialogResult.OK Then
FileName = fd.FileName
Ext = IO.Path.GetExtension(FileName)
'read file
If System.IO.File.Exists(FileName) = True Then
Dim objReader As New System.IO.StreamReader(FileName)
Do While objReader.Peek() <> -1
TextLine = TextLine & objReader.ReadLine & vbNewLine
Loop
TextFile.Text = TextLine
Else
MsgBox("File Does Not Exist")
End If
Me.BtnSort.Enabled = True
Me.BtnDestroy.Enabled = True
'counter = counter + 1
End If
End Sub
Asked
Active
Viewed 142 times
-1

Florian Schmidinger
- 4,682
- 2
- 16
- 28
-
1On which line do you get the exception? fd.Filter = ... ??? – Florian Schmidinger Mar 12 '15 at 05:58
-
possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Chris Dunaway Mar 12 '15 at 15:05
-
Please tell where the exception occurs. – Chris Dunaway Mar 12 '15 at 15:06
2 Answers
0
My guess is that the reference named fd
is set to null (nothing) so you need to instantiate a new OpenFileDialog and assign it to the reference:
fd = New OpenFileDialog()
Since you seem to want to read the whole text of the file anyway you can shorten this code (File.Exists allready returns a boolean value so you dont need to compare with true):
If System.IO.File.Exists(FileName) Then
TextLine = TextLine & System.IO.File.ReadAllText(FileName)
TextFile.Text = TextLine
Else

Florian Schmidinger
- 4,682
- 2
- 16
- 28
0
you need to create an instance for your fd before to use it. Maybe in te constructor or in it's declaration itself:
Private fd As New Form1

alex
- 323
- 1
- 2
- 16