Solved

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

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Replacing HTML tags in Textarea/Textbox 5 32
sync conflicts 1 27
Clone Visual studio 2013 solution 5 17
server plus 2 47
A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
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…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now