Solved

Streamreader - Object reference not set to an instance of an object

Posted on 2007-03-22
2
703 Views
Last Modified: 2010-05-18
Hi
I have the following code in a button click event
Dim sr As StreamReader
        Dim sw As New StreamWriter(btnSWFolder.Text & "\" & lstSWFiles.Text, True)
        Dim sLine As String = ""
        Dim sDiv As String = ""

        For Each subor As String In My.Computer.FileSystem.GetFiles(btnSRFolder.Text)
            Try
                sr = New StreamReader(subor)
                Do
                    sLine = sr.ReadLine()
                    If sLine.StartsWith("ORDER JOURNAL") Then
                        sDiv = Mid(sLine, 57, 2)
                        sw.WriteLine(sDiv)
                    End If
                Loop Until sLine Is Nothing
                sr.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        Next
        sw.Close()
    End Sub

streamreader reads the string (sDiv) and the streamwriter attaches it to a text file. Everything works fine. Only if it hadn't been for that annoying message : Object reference not set to an instance of an object.
When I removed Try ... Catch, VB Studio 2005 stopped and highlighted the line
     If sLine.StartsWith("ORDER JOURNAL") Then
and the explanatory note than gopes underneath showed in the blue bar the following:
    NullReferenceException was unhandled

What's wrong? Why do I get the message when everything works fine?
Thanks
Fero
0
Comment
Question by:Fero45
2 Comments
 
LVL 7

Accepted Solution

by:
jeepstyx22 earned 125 total points
ID: 18774286
Post below obtained from (8th post down): http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1053653&SiteID=1

The problem is as follows:
streamReader.ReadLine will return a null reference (Nothing) at the end of the stream.
You then try to evaluate reader.StartsWith(...) when reader is a null reference.
So you need to check for a null reference (Nothing) before attempting to evaluate reader.StartsWith(...).
There are several ways of doing this, for example:            

Do Until reader Is Nothing OrElse reader.StartsWith("Job Log")
...
Loop

 
0
 
LVL 6

Author Comment

by:Fero45
ID: 18774381
Hi jeepstyx22:
I accepted your answer, thanks. But I have a similar code and never get that message, Just for info
Dim sr As StreamReader
        Dim sw As StreamWriter
        Dim sRadek As String = ""
        Dim sezona As String = ""
        Dim farba As String = ""
        Dim sDiv As String = ""
        Dim subdiv As String = ""
        Dim sDatum As String = ""

        Try
            sw = New StreamWriter(btnSWFolder.Text & "\" & lstSWFiles.Text, True)
            sr = New StreamReader(btnSRFolder.Text & "\" & lstSRFiles.Text)

            Do
                sRadek = sr.ReadLine()

                If sRadek.StartsWith("Output") Or _
                    sRadek.Length = 0 Or sRadek.StartsWith("STYL") Or _
                    sRadek.StartsWith("*") Then
                    'skip

                ElseIf sRadek.StartsWith("BOOKING") Then
                    sDiv = Mid(sRadek, 40, 2)

                ElseIf sRadek.StartsWith("DATE") Then
                    sDatum = Mid(sRadek, 7, 8)

                Else
                    subdiv = fn_div_subdiv(sDiv, Mid(sRadek, 1, 4))
                    sezona = fn_carry_spring07_other(Mid(sRadek, 1, 4))

                    If sezona = "Other" Then
                        'skip
                    Else
                        farba = fn_farba(Mid(sRadek, 7, 2))
                        sw.WriteLine(sDatum & "=" & subdiv & "=" & Mid(sRadek, 1, 4) & "=" & _
                                        farba & "=" & Mid(sRadek, 10, 6) & "=" & _
                                        Trim(Mid(sRadek, 18, 4)) & "=" & Trim(Mid(sRadek, 23, 4)) & "=" & _
                                        Trim(Mid(sRadek, 28, 4)) & "=" & Trim(Mid(sRadek, 33, 4)) & "=" & _
                                        Trim(Mid(sRadek, 38, 4)) & "=" & Trim(Mid(sRadek, 43, 4)) & "=" & _
                                        Trim(Mid(sRadek, 48, 4)) & "=" & Trim(Mid(sRadek, 53, 4)) & "=" & _
                                        Trim(Mid(sRadek, 58, 4)) & "=" & Trim(Mid(sRadek, 63, 4)) & "=" & _
                                        Trim(Mid(sRadek, 68, 4)) & "=" & Trim(Mid(sRadek, 76, 4)) & "=" & _
                                        sezona)
                    End If

                End If
            Loop Until sRadek.Contains("R A N K    B Y    S T Y L E")

            sr.Close()
            sw.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        MessageBox.Show(sDiv & " done!")

Thanks
Fero
0

Featured Post

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Learn how to PXE Boot both BIOS & UEFI machines with DHCP Policies and Custom Vendor Classes
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question