473,473 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

StreamWriter, FileStream throwing uncatchable NullReferenceException

Hello,

I maintain an application that pulls data from a web service and writes
it to an Excel sheet. Originally, the app used OleDb to write the
Excel. Exports ran fine except that some text fields were truncated due
to the 255 character limit of the Jet Excel driver. To overcome this
limit, I decided to just generate CSV directly. This is where my
trouble began.

First I tried the StreamWriter class, implemented as per the "How to:
Write to a Text File" MSDN example
(http://msdn2.microsoft.com/en-us/library/6ka1wd3w.aspx).

Then I tried the FileStream class, implemented as per the example from
the MSDN class entry
(http://msdn.microsoft.com/library/de...ClassTopic.asp)

With both classes, the app would throw a NullReferenceException at an
arbitrary point of the export. Sometimes it would happen on the 400th
record, sometimes on the 1250th, etc. The reason I say it is an
uncatchable exception is because of my work with the debugger. Here is
the chunk of code that the debugger points to when it throws the
exception in debug mode:

For p As Integer = 1 To totalPages ''main record loop
''---
''EXPORT CODE HERE
''---
Catch ex As Exception
swrLogFile.WriteLine("Error while processing Item:")
swrLogFile.WriteLine(ex.Message)
swrLogFile.WriteLine(ex.StackTrace)
swrLogFile.WriteLine("") <<========debugger points here as source
of exception
End Try
Next

I set a breakpoint on the first line of the Catch block to see what
exception it was catching. It never hit the breakpoint. Just jumped
straight to the last line of the Exception block, as shown.
Technically, this seems impossible, so I began to suspect that the
NullReferenceException was just confusing the debugger and it was
pointing to a somewhat arbitrary point in the code.

I then added the following code to test this theory and make sure that
it wasn't my log file StreamWriter that was crashing:

For p As Integer = 1 To totalPages ''main record loop
''---
''EXPORT CODE HERE
''---
Catch ex As Exception
Try
swrLogFile.WriteLine("Error while processing Item:")
swrLogFile.WriteLine(ex.Message)
swrLogFile.WriteLine(ex.StackTrace)
swrLogFile.WriteLine("")
Catch ex2 as Exception
End Try <<==============debugger points here as source of exception
End Try
Next

The test confirmed that the debugger is just jumping to the end of the
catch block for lack of a more accurate place to point.

I've worked with many debuggers that do this frequently; newer MS
debuggers are pretty good at pinpointing the actual source of an
exception, but in this case I think it is failing to do so.

To recap:
a) I am almost 100% positive this is a problem with the managed FileIO
because the code was working fine before I began writing the data
directly to file (vs. through the OleDb driver) and nothing else
changed
b) I have no way of diagnosing and working around this problem because
the compiler fails to give me any useful information about the
exception.

Please help if you can.

Thanks,

-Paul

Sep 1 '06 #1
1 4014
GS
huh,,,, I must be thick, where is the try clause? right after for line ?

<pa*******@gmail.comwrote in message
news:11**********************@74g2000cwt.googlegro ups.com...
Hello,

I maintain an application that pulls data from a web service and writes
it to an Excel sheet. Originally, the app used OleDb to write the
Excel. Exports ran fine except that some text fields were truncated due
to the 255 character limit of the Jet Excel driver. To overcome this
limit, I decided to just generate CSV directly. This is where my
trouble began.

First I tried the StreamWriter class, implemented as per the "How to:
Write to a Text File" MSDN example
(http://msdn2.microsoft.com/en-us/library/6ka1wd3w.aspx).

Then I tried the FileStream class, implemented as per the example from
the MSDN class entry
(http://msdn.microsoft.com/library/de...-us/cpref/html
/frlrfSystemIOFileStreamClassTopic.asp)
>
With both classes, the app would throw a NullReferenceException at an
arbitrary point of the export. Sometimes it would happen on the 400th
record, sometimes on the 1250th, etc. The reason I say it is an
uncatchable exception is because of my work with the debugger. Here is
the chunk of code that the debugger points to when it throws the
exception in debug mode:

For p As Integer = 1 To totalPages ''main record loop
''---
''EXPORT CODE HERE
''---
Catch ex As Exception
swrLogFile.WriteLine("Error while processing Item:")
swrLogFile.WriteLine(ex.Message)
swrLogFile.WriteLine(ex.StackTrace)
swrLogFile.WriteLine("") <<========debugger points here as source
of exception
End Try
Next

I set a breakpoint on the first line of the Catch block to see what
exception it was catching. It never hit the breakpoint. Just jumped
straight to the last line of the Exception block, as shown.
Technically, this seems impossible, so I began to suspect that the
NullReferenceException was just confusing the debugger and it was
pointing to a somewhat arbitrary point in the code.

I then added the following code to test this theory and make sure that
it wasn't my log file StreamWriter that was crashing:

For p As Integer = 1 To totalPages ''main record loop
''---
''EXPORT CODE HERE
''---
Catch ex As Exception
Try
swrLogFile.WriteLine("Error while processing Item:")
swrLogFile.WriteLine(ex.Message)
swrLogFile.WriteLine(ex.StackTrace)
swrLogFile.WriteLine("")
Catch ex2 as Exception
End Try <<==============debugger points here as source of exception
End Try
Next

The test confirmed that the debugger is just jumping to the end of the
catch block for lack of a more accurate place to point.

I've worked with many debuggers that do this frequently; newer MS
debuggers are pretty good at pinpointing the actual source of an
exception, but in this case I think it is failing to do so.

To recap:
a) I am almost 100% positive this is a problem with the managed FileIO
because the code was working fine before I began writing the data
directly to file (vs. through the OleDb driver) and nothing else
changed
b) I have no way of diagnosing and working around this problem because
the compiler fails to give me any useful information about the
exception.

Please help if you can.

Thanks,

-Paul

Sep 11 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Viorel | last post by:
About FileStream and StreamWriter!? I have a string1 which contains(of course in unicode) a html code with Unicode charset specified in it. 1) bytecontent=...
33
by: | last post by:
Hi, When constructing StreamWriter with the following.. FileStream f = new FileStream(..); StreamWriter s = new StreamWriter(f); Then attempt to write out åäö letters they become garbage. ...
4
by: Dan | last post by:
In the following example, is it necessary to close the FileStream object as well as the StreamWriter object? FileStream fs = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write,...
10
by: Oscar Thornell | last post by:
Hi, I generate and temporary saves a text file to disk. Later I upload this file to Microsoft MapPoint (not so important). The file needs to be in UTF-8 encoding and I explicitly use the...
5
by: xuxu | last post by:
If you use StreamWrite Write or WriteLine it causes Session to End! e.g. FileStream fs = File.Open(fileName,FileMode.Append,FileAccess.Write); StreamWriter sw = new StreamWriter(fs);...
3
by: Mads Westen | last post by:
Hi, I'm making some changes to my program, because I need to write with Codepage 850. Before I used FileStream, but I found code to use with StreamWriter instead. My problem is, that I don't...
3
by: Rob | last post by:
I have a streamwriter that may get closed. The writer is passed into another class by ref and is consumed there. What can happen is that the writer might get closed by the spawning class. Is...
1
by: iwdu15 | last post by:
hi...just a quick question. what are the differences in using a FileStream and StreamWriter opposed to just a StreamWriter.....for instance Dim fs as New FileStream("C:\Test.txt",...) Dim sw As...
3
by: stumorgan | last post by:
I'm doing some USB communications in C# and am running into a minor annoyance. I'm using the Windows API CreateFile function to get a SafeFileHandle, which I then stuff into a FileStream and from...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.