473,545 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

StreamWriter, FileStream throwing uncatchable NullReferenceEx ception

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 NullReferenceEx ception 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.Writ eLine("Error while processing Item:")
swrLogFile.Writ eLine(ex.Messag e)
swrLogFile.Writ eLine(ex.StackT race)
swrLogFile.Writ eLine("") <<========debug ger 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
NullReferenceEx ception 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.Writ eLine("Error while processing Item:")
swrLogFile.Writ eLine(ex.Messag e)
swrLogFile.Writ eLine(ex.StackT race)
swrLogFile.Writ eLine("")
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 4027
GS
huh,,,, I must be thick, where is the try clause? right after for line ?

<pa*******@gmai l.comwrote in message
news:11******** **************@ 74g2000cwt.goog legroups.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
/frlrfSystemIOFi leStreamClassTo pic.asp)
>
With both classes, the app would throw a NullReferenceEx ception 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.Writ eLine("Error while processing Item:")
swrLogFile.Writ eLine(ex.Messag e)
swrLogFile.Writ eLine(ex.StackT race)
swrLogFile.Writ eLine("") <<========debug ger 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
NullReferenceEx ception 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.Writ eLine("Error while processing Item:")
swrLogFile.Writ eLine(ex.Messag e)
swrLogFile.Writ eLine(ex.StackT race)
swrLogFile.Writ eLine("")
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
6050
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= System.Text.UnicodeEncoding.Unicode.GetBytes(string1);
33
4324
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. BUT
4
28322
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, FileShare.None); StreamWriter swFromFile = new StreamWriter(logFile); swFromFile.Write(textToAdd); swFromFile.Flush(); swFromFile.Close();
10
4625
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 "Encoding.UTF8" in the constructor like this: StreamWriter writer = new StreamWriter(file, Encoding.UTF8); When I do this the StreamWriter inserts an...
5
1878
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); sw.Write("test"); sw.Close(); fs.Close(); Now all session information is lost!! Is there a work around for this ?
3
15683
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 get any output when writing with StreamWriter, but only with FileStream. I got StreamWriter to work in another class, where I only have 1 Writeline
3
2977
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 there a way to extract the path of the streamwriter passed in by ref? I don't see a property for it, but there might be a work around someone has. ...
1
1619
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 New StreamWriter(fs) sw.WriteLine("Stuff") sw.Flush() sw.Close()
3
5219
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 there into StreamReader and StreamWriter objects. The StreamReader is working beautifully, but the StreamWriter isn't. If I convert the...
0
7416
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7932
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7442
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7776
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6001
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5347
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4965
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3473
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1905
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 we have to send another system

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.