Object Reference Error

Bigbadborris

Active member
Joined
Aug 21, 2013
Messages
35
Programming Experience
Beginner
Hi all

I have this piece of code that organizes a text file created by File-pro into a more reasonable format. The code works perfect however I get the following message.

"Object Reference not set to an instance of an object"

Can someone please tell me where i have gone wrong.

Many Thanks

VB.NET:
Imports System.IO

Public Class Form1


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim File_NameIN As String = "C:\Users\Paul Murphy\Documents\DataFiles\CUSTKEY.txt"
        Dim File_NameOUT As String = "C:\Users\Paul Murphy\Documents\DataFiles\CUSTKEYA.txt"


        Try
            Using SR As New StreamReader(File_NameIN)
                Using SW As New StreamWriter(File_NameOUT)
                    Do
                        Dim strLine As String = SR.ReadLine()
                        Dim strLine2 As String
                        For i As Integer = 1 To strLine.Length Step 581
                            strLine2 = strLine.Remove(0, 601)
                            SW.WriteLine(Mid(strLine2, i, 581))
                        Next i
                    Loop
                End Using
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
            Exit Sub
        End Try
        MsgBox(File_NameOUT & " written")
    End Sub


End Class
 
Solved it :adoration:

Change my Do statement to Do Until SR.endofstream

Not sure if the is the right way but it worked like a dream.

VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim File_NameIN As String = "C:\Users\Paul Murphy\Documents\DataFiles\CUSTKEY.txt"
        Dim File_NameOUT As String = "C:\Users\Paul Murphy\Documents\DataFiles\CUSTKEYA.txt"


        Try
            Using SR As New StreamReader(File_NameIN)
                Using SW As New StreamWriter(File_NameOUT)
                    Do Until SR.EndOfStream
                        Dim strLine As String = SR.ReadLine()
                        Dim strLine2 As String
                        For i As Integer = 1 To strLine.Length Step 581
                            strLine2 = strLine.Remove(0, 601)
                            SW.WriteLine(Mid(strLine2, i, 581))
                        Next i
                    Loop
                End Using
            End Using
                Catch ex As Exception
            MsgBox(ex.Message)
            Exit Sub
        End Try
        MsgBox(File_NameOUT & " written")


    End Sub
 
Yes, that is the correct way. You only want to read lines until you reach the end of the stream and that's exactly what you are now doing. By the way, rather than doing this:
Using var1 As SomeType
    Using var2 As SomeOtherType
        '...
    End Using
End Using
you should do this:
Using var1 As SomeType,
      var2 As SomeOtherType
        '...
End Using
It still works exactly the same way but the code is a bit more readable. You'd only use multiple nested Using statements if you wanted to put some code between the two Using lines.

Also, rather than doing this:
Try
    '...
Catch ex As Exception
    MsgBox(ex.Message)
    Exit Sub
End Try

MsgBox(File_NameOUT & " written")
I would suggest that you do this:
Try
    '...
    Debug.WriteLine(File_NameOUT & " written")
Catch ex As Exception
    Debug.WriteLine(ex.Message)
End Try
Firstly, you get rid of a pointless Exit statement by placing code that should only be run if no exception is thrown in a position that will only be reached if no exception is thrown. Secondly, by using Debug.WriteLine instead of MsgBox, you allow yourself to simply leave that code as it is without having to remove bits of it intended only for debugging. Any use of the Debug class will simply be ignored when you compile a Release build. The debug output will also appear in the Output window of your IDE, rather than changing the flow of your application during debugging.
 
Back
Top