Solved

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

Posted on 2007-03-22
2
702 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Our Group Policy work started with Small Business Server in 2000. Microsoft gave us an excellent OU and GPO model in subsequent SBS editions that utilized WMI filters, OU linking, and VBS scripts. These are some of experiences plus our spending a lo…
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 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