Results 1 to 3 of 3

Thread: [RESOLVED] [2005] Variable 'myStreamReader' is used before it has been assigned a value

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    Resolved [RESOLVED] [2005] Variable 'myStreamReader' is used before it has been assigned a value

    I am receiving the following warning:

    Warning 1 Variable 'myStreamReader' is used before it has been assigned a value. A null reference exception could result at runtime.
    Here's my code:

    Code:
    Public Sub loadData()
            'this reads the textfile and loads the checkbox
            Dim currentFields() As String
            ' The StreamReader must be defined outside of the Try-Catch block
            '   in order to reference it in the Finally block.
    
            Dim myStreamReader As StreamReader
            Dim myInputString As String
            Dim rowCount As Integer = 0
    
            ' Ensure that the creation of the new StreamReader is wrapped in a 
            '   Try-Catch block, since an invalid filename could have been used.
            Try
                ' Create a StreamReader using a Shared (static) File class.
                myStreamReader = File.OpenText(filePath)
                ' Begin by reading a single line
                myInputString = myStreamReader.ReadLine()
                ' Continue reading while there are still lines to be read
                While Not myInputString Is Nothing
                    currentFields = Split(myInputString, ",")
                    If currentFields(1) = "0" Then
                        lstApts.Items.Add(currentFields(0))
                    Else
                        lstApts.Items.Add(currentFields(0), True)
                    End If
                    rowCount += 1
                    ' Read the next line.
                    myInputString = myStreamReader.ReadLine()
                End While
            Catch exc As Exception
                ' Show the error to the user.
                MsgBox("File could not be opened or read." + vbCrLf + _
                    "Please verify that the filename is correct, " + _
                    "and that you have read permissions for the desired " + _
                    "directory." + vbCrLf + vbCrLf + "Exception: " + exc.Message)
            Finally
                ' Close the object if it has been created.
                If Not myStreamReader Is Nothing Then
                    myStreamReader.Close()
                End If
            End Try
        End Sub
    Warning caused on If statement of Finally block.

  2. #2
    Lively Member SharkC382's Avatar
    Join Date
    Jul 2007
    Location
    Kennesaw, Ga
    Posts
    68

    Re: [2005] Variable 'myStreamReader' is used before it has been assigned a value

    This is just a warning that mystreamreader MAY NOT be anything by the time it reaches the Finally block.
    You have mystreamreader DIMed as a streamreader, but it doesnt mean anything until you assign it with the File.OpenText method. If that were to fail, then mystreamreader would still be uninitialized when it reaches the Finally block.

    That's why there's a warning. It's okay.
    Matt
    VB2003/5 and Orcas
    2 Athlon 64s with 5 monitors for programming and work
    multiple linux machines for databases and testing
    multiple vmware (ESX) virtual machines for testing
    HP-UX, MPE on HP9000 and HP3000

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Variable 'myStreamReader' is used before it has been assigned a value

    You can get rid of that warning message by changing this:
    vb.net Code:
    1. Dim myStreamReader As StreamReader
    to this:
    vb.net Code:
    1. Dim myStreamReader As StreamReader = Nothing
    The IDE gives you the warning because it doesn't know whether you intended that variable to be Nothing or you simply forgot to assign a value to it. By explicitly setting the variable to Nothing you are telling the compiler that it is Nothing because you intended it to be Nothing and that you will take responsibility for making sure that it is not Nothing if and when it is accessed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width