nibsy Posted July 18, 2003 Posted July 18, 2003 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 Quote
Heiko Posted July 18, 2003 Posted July 18, 2003 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. Quote .nerd
nibsy Posted July 18, 2003 Author Posted July 18, 2003 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)? Quote
*Gurus* divil Posted July 18, 2003 *Gurus* Posted July 18, 2003 Yes, do that, but also in the finally block, do a check for a null reference as follows: If Not fsInput Is Nothing Then fsInput.Close() Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Leaders dynamic_sysop Posted July 18, 2003 Leaders Posted July 18, 2003 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 Quote
nibsy Posted July 18, 2003 Author Posted July 18, 2003 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.