Null Reference Exception
I am having a problem with a multi-form application I am writing in VB .Net 2003. When I transition back from one form to my startup form, I get an intermittent Null Reference Exception. Sometimes it will work, moving back and forth from form to form with no problems for 15-20 minutes, and sometimes it will crash everytime. I have tried putting a try-catch block around the instantiation of the main form. It does not catch anything. The exception points to line 2 of the startup form, which reads:
Public Class frmWelcome
Not much to cause a problem there, I would have thought. I am attaching the code for bringing up the welcome screen in hopes that someone will see something I am not thinking of.
Try
Dim welcomeAsNew frmWelcomewelcome.Show()
Catch exAs ExceptionMsgBox(ex.Message, MsgBoxStyle.OKOnly, "Error")
EndTryMe.Close()The exact error that I am getting is:
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.
I also tried putting a try-catch block around the load method of the welcome form, thinking it might be in there. Nothing. I am stumped. I would appreciate some help with this.
Thanks
Joe White
[2045 byte] By [
JWhite] at [2007-12-23]
Here is the complete code forthe exit of the second form.
Private
Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Clickkey.Close()
TryDim welcome As New frmWelcomewelcome.Show()
Catch ex As ExceptionMsgBox(ex.Message, MsgBoxStyle.OKOnly, "Error")
End TryMe.Close()End SubKey.close is a method call closing a registry key I have open on that form.
Thanks
Joe
I am creating a frmWelcome form from inside the frmOperator form. I was using Me.Hide method to hide the Operator form, rather than closing it. I tried hiding the operator form, to preserve it, but that did not help at all.
Joe
if you do not have to hide the form, you maybe better of using ShowDialog() rather than Show() as ShowDialog() will keep displaying that form and not let any forms from its caller be able to be accessed until the form is closed and only then will the caller get back into action.
how are you calling the form (frmWelcome) - meaning, where abouts in the frmOperator form?
That does not help. I am still getting an unhandled exception Null Reference Exception at that point. I'm still mystified as to where the exception is coming from, since I have try-catch blocks around everything, but it never catches anything. I have try catch blocks around the form creation, the old form destruction, and the new form load. No catches anywhere. I'm at a loss.
Thanks to all for your help. After some more searches on the internet, I found some things out. Evidentally, some users have experienced problems with controls with transparent backgrounds. I was using a control on one form that had a transparent background. After setting the backcolor to a definite color, this error has apparently left the building.
Thanks
Ah, the joys of debugging. Everyone is trying to go down the logical route to determine the source of the problem which turns up to be something completely different.
FWIW, I often find bound controls that are not properly disposed, or where the control is disposed prior to the binding source, can cause this issue as well. In the future, you might try turning on the option to stop on all errors, not just unhandled ones. Another trick I have used in the past is to attach a second instance of VS to the one with the project and turning on stop on all errors. Unfortunatly, this trick does not seem to work in 2005 any more.
Glad you were able to track down the source of the problem this time.
Jim Wooley
http://devauthority.com/blogs/jwooley