Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A NullReferenceException guaranteed to have you scratching your head 1

Status
Not open for further replies.

RebLazer

Programmer
Jun 7, 2002
438
US
I have a VB.NET Windows form that I've been developing for about a month. The app consists of several forms. Aside from all of the usual Windows Forms controls manipulation (i.e. the GUI), it executes some simple SQL statements against a SQL Server 2000 database that is on the network, and runs a couple of Crystal Reports on that data. When I say "simple SQL statements," I mean in the order of "select * from
" which returns 8000 or so small records.

I was testing it over and over and over again :) before distributing it - and I got this error:[tt]
An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll
Additional information: Object reference not set to an instance of an object.[/tt]

I'm no expert developer, but I've been around the block around enough times to know what should cause a NullReferenceException and what should not. I could run the identical Crystal Reports report 50 times (literally) over and over before I get this exception. Sometimes it happens after just a few times, and sometimes it takes dozens of times. But the important point here is that I am doing exactly the same three steps over and over:
starting at my main menu,
clicking my "Run Report" button, and
once the report comes up, clicking my "Go Back to Main Menu" button
and repeating that process.

When the error occurs, it occurs on the red line, below:[tt]
Dim view As New View
Me.Hide()
view.Show() [/tt]
which is the line that displays a new form called "view".

Allegedly, there is something null on that form. There are four objects on that form:
a button,
a label,
a panel, and
a CrystalReportsViewer

If one had to guess the null culprit, one would certainly suspect the CrystalReportsViewer.

Once, I got a different error (although I can't tell if it was on the same line as I was running in Release mode):[tt]
An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in crystaldecisions.crystalreports.engine.dll
Additional information: External component has thrown an exception.[/tt]

I haven't a clue what that means altogether...

Back to the more common exception... Why would replicating the same scenario repeatedly - on completely static data sometimes be null and 98% of the time work?

Thank you very much - this one has me totally baffled!
Lazer
 
Are you calling .Dispose on your CR objects when you're done with them?

Remember that .NET is garbage collected, so it will probably not free the object when you think it did -- usually sometime later. Calling .Dispose causes it to happen synchronously.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Chiph,

Thanks for your reply.

No, I'm not currently doing that. Which object should I dispose - the CrystalReportsViewer?

But even if I need to call Dispose, how would you explain why replicating the same scenario repeatedly - on completely static data sometimes be null and almost all of the time work?

Lazer
 
Chiph / all,

I now call Dispose() on all "suspect" objects and it seems much better. Without Dispose, out of about 30 times, it crashed 5 times. With Dispose, out of 30 times, it didn't crash at all.

Now don't get me wrong - I do not understand how a garbage collection issue leads to a NULL exception...

But in the meantime, thank you very much, Chiph! [star]

Lazer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top