0

i watched a video tutorial and saw the guy write and execute the code below and worked but when i try to compile mine it says "object reference not set to an instance of an object". i have tried several things to see if i can get what the problem is but to no avail.

Imports System.IO
Public Class Form2

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim myline = New StreamReader("TextFile1.txt")
            Dim line As String = ""
            While Not IsNothing(line)
                line = myline.ReadLine
                If IsNothing(line) Then
                    TextBox2.AppendText(line)
                End If
            End While
            myline.Close()

        Catch ex As Exception

            TextBox2.AppendText(ex.Message)
            MsgBox(ex.Message)

        End Try


    End Sub

End Class

can anyone help please? thanks

okojie ernest
  • 11
  • 1
  • 1
  • 5

5 Answers5

3

I think you are missing a Not:

            If Not IsNothing(line) Then
                TextBox2.AppendText(line)
            End If
Markus
  • 20,838
  • 4
  • 31
  • 55
  • Thanks alot. it worked. i am from php coding and this kind of "If Not IsNothing:" is strange kind of. so my mind didnt think that could be the problem. Thanks alot – okojie ernest Dec 29 '13 at 20:16
2

this kind of "If Not IsNothing:" is strange kind of.

IsNothing is a VB-ism as opposed to NET syntax. Other ways of coding it include:

If String(line).IsNullOrEmpty = False Then 
' or 
If Not String(line).IsNullOrEmpty Then 
' which is the same type of garble as Not IsNothing

also:
If Line IsNot Nothing Then

Since IsNothing is a VB function returning a Boolean, you can also just evaluate it:

If IsNothing(line) = False Then ...
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
1

In the condition If IsNothing(line) Then you are missing a Not.

If Not IsNothing(line) Then
    TextBox2.AppendText(line)
End If
Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
0

Well, it's simple enough :) Just missing your object definition :)

Dim myline = New StreamReader("TextFile1.txt")

-doesnt define myline. It's like your doing this:

Dim variable = 1

You have to say:

Dim myline as StreamReader = New StreamReader("TextFile1.txt")

So that Visual Basic knows what kind of variable you're making before you stuff it with a value. There are better, more acute ways of reading files in VB.NET (Personally my favorite language :) ), but I'm sure this will do for your application.

If you end up wanting to read an entire file to an array (list), you can look through snippets to find the code for reading a file to a string, then split it to a list with the anystring.split command and the "vbCRLF" character (An endline, like your enter button).

0

Seems like it would be easier to just call the ReadAllText method:

Imports System.IO

Public Class Form2

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            TextBox2.AppendText(File.ReadAllText("TextFile1.txt"))
        Catch ex As Exception
            TextBox2.AppendText(ex.Message)
            MsgBox(ex.Message)
        End Try
    End Sub

End Class
Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48