Solved

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

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

The problems with reply email signatures

Do you wish that you could place an email signature under a reply? Well, unfortunately, you can't. That great Exchange/Office 365 signature you've created will just appear at the bottom of an email chain. What a pain! Is there really no way to solve this? Well, there might be...

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
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…
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…

920 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

16 Experts available now in Live!

Get 1:1 Help Now