Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I can't see why I get the following error, on the highlighted line in the code below, when I try to open a file that does not exist:

 

An unhandled exception of type 'System.NullReferenceException' occurred in WebTrendsLogs.exe

 

Additional information: Object reference not set to an instance of an object.

 

 

Dim fsInput As FileStream 
Try
'Open the file.
fsInput = New FileStream(sreadFile, FileMode.Open, FileAccess.Read)
 
Try
	srInput = New StreamReader(fsInput)
               'Read File.
               sFileLine = srInput.ReadLine()
               While Not IsNothing(sFileLine)
		'Append to array.
               	sContents.Add(sFileLine)
               	'Read File.
                       sFileLine = srInput.ReadLine()
               End While
       Catch eIO As Exception
               MessageBox.Show("Couldn't read the file " & sFile, "Error Message", _
                                        MessageBoxButtons.OK, MessageBoxIcon.Error)
       Finally
               srInput.Close()
       End Try
Catch eFile As Exception
       MessageBox.Show("Couldn't open file " & sFile, "Error Message", _
                      MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
       [color=red] fsInput.Close() [/color]
End Try

 

Any assistance will be greatfully received, Thanks

Posted

Makes sense to me:

 

An instance of Filestream (with the name fsInput) can not be created.

 

An exception is thrown. (eFile)

 

In the finally block, there still is no instance of Filstream called fsInput. fsInput is still nothing.

.nerd
Posted

Thanks Heiko,

 

I guess the best thing to do would not to error trap in this way, but to first check if the file exists (IO.File.Exists)?

  • Leaders
Posted

you dont need quite so many Try / Catches in there , here's a simple example that has less lines of code and works without a problem :

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       Dim sReader As StreamReader
       Try
           sReader = New StreamReader(New FileStream("C:\test.txt", FileMode.Open, FileAccess.Read))
           While Not sReader.Peek
               MessageBox.Show(sReader.ReadLine)
           End While
       Catch ex As Exception
           MessageBox.Show(ex.Message)
       Finally
           sReader.DiscardBufferedData()
           sReader.Close()
           sReader = Nothing
       End Try
   End Sub

Posted

Thanks all, I just thought I would post my working, streamlined code:

 

Try
 'Open the file. 
 srInput = New StreamReader(New FileStream(sreadFile, _
                                                           FileMode.Open, FileAccess.Read))
 'Read the file.
 sFileLine = srInput.ReadLine()
 While Not IsNothing(sFileLine)
      'Append to array.
      sContents.Add(sFileLine)
      'Read Line.
      sFileLine = srInput.ReadLine()
 End While
Catch ex As Exception
   MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, _
                                                                           MessageBoxIcon.Error)
Finally
  If Not srInput Is Nothing Then
    srInput.DiscardBufferedData()
    srInput.Close()
    srInput = Nothing
  End If
End Try

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...