help me_about a problem of reading data from a file

first i want to load the txt_file data to a array when loading a application form, code like this:

Try
            Using sr As StreamReader = My.Computer.FileSystem.OpenTextFileReader("contrast.txt")
                Dim line As String
                Do
                    line = sr.ReadLine()
                    doorno = line.Substring(0, 4).Trim()
                    camerano = line.Substring(5).Trim()
                    count += 1
                    contrastTable(count, 0) = doorNo
                    contrastTable(count, 1) = cameraNo
                Loop Until line Is Nothing
                monitor = CType(contrastTable(0, 1), Integer)
                sr.Close()
            End Using
        Catch ex As Exception
            MessageBox.Show("The file could not be read:" + ex.Message)
End Try

but when i run the application, a error_messagebox pops up and says:

the file could not be read: Object reference not set to an instance of an object.

 

what can i do to remove this bug!

all your help is precious!



Answer this question

help me_about a problem of reading data from a file

  • Sayed Zeeshan

    i have resolved this problem.code as follows:

    Imports system.io
    Public Class 禁 系

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    '打 通信端口
    Try
    DoorPort.Open()
    ADPort.Open()
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try

    '新建日志文件
    Dim filetitle As String = filedate.Year + "_" + filedate.Month + "_" + filedate.Day '日志文件名
    Dim path As String = "logs\" + filetitle + ".txt"
    If File.exists(path) = False Then
    swlog = File.CreateText(path)
    Else
    swlog = File.AppendText(path)
    End If

    AddHandler DoorPort.DataReceived, AddressOf swapevent
    End Sub

    Dim contrastTable(0 To 1000, 0 To 1) As String ' 照表 ,第一 是 ,第二 是 像
    Dim filedate As Date = New Date()
    Dim logtime As String = filedate.Now()
    Dim logdoor As String
    Dim logcamera As String
    Dim logmonitor As String
    Dim doorno As String '定
    Dim camerano As String '定 像
    Dim count As Integer = 0 '定 照表的行
    Dim swlog As StreamWriter
    Dim monitor As Integer '定 器
    Dim outputstring1 As String
    Dim outputstring2 As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' 取 照表到一 中
    Try
    Using sr As StreamReader = New StreamReader("contrast.txt")
    Dim line As String
    While sr.Peek <> -1
    line = sr.ReadLine()
    doorno = line.Substring(0, 4).Trim()
    camerano = line.Substring(5).Trim()
    contrastTable(count, 0) = doorno
    contrastTable(count, 1) = camerano
    count += 1
    End While
    monitor = CType(contrastTable(0, 1), Integer)
    sr.Close()
    End Using
    Catch ex As Exception
    ' Let the user know what went wrong.
    MessageBox.Show(ex.Message)
    End Try

    End Sub

    Private Function findCameraNo(ByVal doorno2 As String) As String
    Dim i As Integer
    Dim camerano2 As String
    For i = 0 To count
    If contrastTable(i, 0) = doorno2 Then
    camerano2 = contrastTable(i, 1)
    End If
    Next
    Return camerano2
    End Function

    Private Sub swapevent(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
    ' 取 禁系 的
    Dim line As String
    Dim monitor2 As Integer
    line = DoorPort.ReadLine()
    doorno = line.Substring(0, 3)
    ' 的 像
    camerano = findCameraNo(doorno)
    ' 循 示
    If monitor2 = -1 Then
    monitor2 = monitor
    End If
    outputstring1 = monitor2 + " M" + " a"
    outputstring2 = camerano + " #" + " a"
    ADPort.Write(outputstring1) ' AD矩 送 器的命令
    ADPort.Write(outputstring2) ' AD矩 送 像 的命令
    monitor2 -= 1

    logdoor = doorno
    logcamera = camerano
    logmonitor = monitor2
    ' 入日志文件
    swlog.WriteLine(logtime + vbTab + logdoor + vbTab + logcamera + vbTab + logmonitor)
    ' 入界面的richtextbox
    RichTextBox1.AppendText(logtime + vbTab + logdoor + vbTab + logcamera + vbTab + logmonitor + vbCrLf)
    End Sub

    Private Sub form1_formclosing(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ADPort.Close()
    DoorPort.Close()
    End Sub
    End Class

    well, although this is done before i took a look at your advices , thanks NateV for your advice all the same.


  • Natural_orange

    en , the problem still occurces

    well ,thanks all the same!


  • TMB

    may be i should write all the code here :

    Public Class

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            '
            Try
                DoorPort.Open()
                ADPort.Open()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

            '
            Dim filetitle As String = filedate.Year + "_" + filedate.Month + "_" + filedate.Day '
            Dim path As String = "logs\" + filetitle + ".txt"
            If File.exists(path) = False Then
                swlog = File.CreateText(path)
            Else
                swlog = File.AppendText(path)
            End If

            AddHandler DoorPort.DataReceived, AddressOf swapevent
        End Sub

        Dim contrastTable(,) As String ' , ,
        Dim filedate As Date = New Date()
        Dim logtime As String = filedate.Now()
        Dim logdoor As String
        Dim logcamera As String
        Dim logmonitor As String
        Dim doorno As String '
        Dim camerano As String '
        Dim count As Integer = 0 '
        Dim swlog As StreamWriter
        Dim monitor As Integer '
        Dim outputstring1 As String
        Dim outputstring2 As String

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '
            Try
                Using sr As StreamReader = My.Computer.FileSystem.OpenTextFileReader("contrast.txt")
                    Dim line As String
                    While sr.Peek <> -1
                        line = sr.ReadLine()
                        doorno = line.Substring(0, 4).Trim()
                        camerano = line.Substring(5).Trim()
                        count += 1
                        contrastTable(count, 0) = doorno
                        contrastTable(count, 1) = camerano
                    End While
                    monitor = CType(contrastTable(0, 1), Integer)
                    sr.Close()
                End Using
            Catch ex As Exception
                ' Let the user know what went wrong.
                MessageBox.Show("The file could not be read:" + ex.Message)
            End Try

        End Sub

        Private Function findCameraNo(ByVal doorno2 As String) As String
            Dim i As Integer
            Dim camerano2 As String
            For i = 0 To count
                If contrastTable(i, 0) = doorno2 Then
                    camerano2 = contrastTable(i, 1)
                End If
            Next
            Return camerano2
        End Function

        Private Sub swapevent(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
            '
            Dim line As String
            Dim monitor2 As Integer
            line = DoorPort.ReadLine()
            doorno = line.Substring(0, 3)
            '
            camerano = findCameraNo(doorno)
            '
            If monitor2 = -1 Then
                monitor2 = monitor
            End If
            outputstring1 = monitor2 + " M" + " a"
            outputstring2 = camerano + " #" + " a"
            ADPort.Write(outputstring1) ' AD
            ADPort.Write(outputstring2) ' AD
            monitor2 -= 1

            logdoor = doorno
            logcamera = camerano
            logmonitor = monitor2
            '
            swlog.WriteLine(logtime + vbTab + logdoor + vbTab + logcamera + vbTab + logmonitor)
            ' richtextbox
            RichTextBox1.AppendText(logtime + vbTab + logdoor + vbTab + logcamera + vbTab + logmonitor + vbCrLf)
        End Sub

        Private Sub form1_formclosing(ByVal sender As System.Object, ByVal e As System.EventArgs)
            ADPort.Close()
            DoorPort.Close()
        End Sub
    End Class


  • Rakesh Kashnia

    Hi,

    This is because you check whether "line" is nothing in the end, which means it will fail on Substring() if line is nothing.

    Here's an alternative way of iterating through all lines of text in a file:

    While sr.Peek <> -1

    line = sr.ReadLine()

    ...

    End While

    Or you can add the check in the beggining(ie by assigning "line" an initial variable and use Do While)



  • ryandailey21

    Hi there,

    The problem you are seeing now (after switching to the method Ryan Tsai suggested) is not your file reading routine but the fact that your two dimensional array of strings (contrastTable) has not been initialised. The implication of this is that you get the exception you're seeing.

    You need to actually define the size of the contrastTable array before you use it. It may be difficult to actually determine what size it will be (since this relies on how many lines are in your file) but you need to set it to something.

    I have attached a modification to your code a bit later in this post, where I have set contrastTable so that is has 1000 "rows" and 2 "columns" but you should, of course, change this as necessary. You could also consider using some sort of other Collection construct but if you're happy with your current setup then it's not necessary (could be slightly better though).

    Also, with the postion of the count += 1 in your file reading code, the implication of putting this statement where you have is that the first "row" of contrastTable (Row Index 0) is never populated and so when you assign the monitor variable like you have in your code:

    monitor = CType(contrastTable(0, 1), Integer)


    It won't contain anything meaningful. In the below example, I have also changed the positioning of your
    count += 1 statement so that the first "row" (Row Index 0) of contrastTable is populated.

    Also, with the way you have defined your StreamReader, you need to be mindful of where you place the text file so that it can be found and read by your program. Just a little word of advice/warning as you might encounter an error if the text file isn't in the place the StreamReader was expecting it to be.

    And one final thing, you don't need the call to sr.Close() as the Using statement will do the clean up for you (and call the Close() function by itself). Doesn't really hurt anyone to keep it in there, though, so I have not touched that function call.

    Anyway, below is the modification I've made to your code, have a go & see if it works:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ReDim contrastTable(1000, 2) 'Make sure the array is usable

        Try
            Using sr As StreamReader = My.Computer.FileSystem.OpenTextFileReader("contrast.txt")
                Dim line As String

                While sr.Peek <> -1
                    line = sr.ReadLine()
                    doorno = line.Substring(0, 4).Trim()
                    camerano = line.Substring(5).Trim()
                    contrastTable(count, 0) = doorno
                    contrastTable(count, 1) = camerano

                    count += 1 'Moved here so that the first row is populated
                End While
                monitor = CType(contrastTable(0, 1), Integer)
                sr.Close()
            End Using
      Catch ex As Exception
        ' Let the user know what went wrong.
        MessageBox.Show("The file could not be read:" + ex.Message)
      End Try

    End Sub

    Hope that helps a bit, but sorry if it doesn't






  • help me_about a problem of reading data from a file