Solved

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

Posted on 2007-03-22
2
706 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

749 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