Error tracking - finding source of messagebox

jwh

Well-known member
Joined
Aug 18, 2006
Messages
155
Programming Experience
3-5
Evening!

I have inherited a project from another programmer, and I am a little stuck.

Basically, during one of the functions, an exception is thrown, and I am presented with a messagebox saying "Object reference not set to an instance of an object".

The message box seems to have been thrown by a Try / Catch statement, but I have no idea where.

Is there a way for me to stop the program execution when the message box appears (bear in mind I have NO idea where in the code this box originates) so that I can figure out why this error is occurring.

Thanks in advance!
 
Search the source code for MessageBox.Show .... put a break point on each one. Run the code, it should hit the break point just before it displys the message box. If you do not hit any of the break points, then you are getting the standard message from the Framework.... in which case, if you look at InnerException, it should tell you where the error is happening.

-tg
 
Theres an easier way than this.. On the Debug menu, click Exceptions

Put a tick next to Common Language Runtime Exceptions (CLRE) in the THROWN column

Now, whenever your project throws an exception, whether it is handled or not, the program will break to the debugger

If there are just too many exceptions thrown (and handled) to make it workable, then go back into Debug..Exceptions. Untick the THROWN box you ticked then EXPAND the CLRE heading, expand System, and look for NullReferenceException - tick it. Every time you get "Object reference not set.." your program will break to the line near where the exception was thrown

Note, if you say in your code:
throw Exception()...

then the program breaks to the line after the throw, because the throw has executed but the nex tline has not (the program breaks)

ANyways.. im sure youl lfigure it out. GOod luck
 
I'm willing to bet that it's not being thrown in a Try .. Catch block. If it is then you need to track down the programmer that wrote that app and beat him until he tells you why on earth he would throw a completlely non descriptive exception like that!!!:D
 
Actually, I'd be willing to bet that it IS occurring within a Try/Catch but the fault of the programmer is that he is Catching a generic Exception. If he were catching soemthing more specific then this type of exception wouldnt be being absorbed by his blanket Catch and the IDE would break at the point the exception occurred because it is unhandled.

Though it is possible for a programmer to throw this exception directly, "Object reference not set to an instance" is a standard error message usually thrown by the CLR without direct instruction from the programmer to do so. It is often the result of programmer error; by failing to forsee a combination of parameters or variables that would lead to a situation where a vital object reference remains uninstantiated, the programmer generates cause for this error to appear. On its most basic level, the error will be thrown by this code:

VB.NET:
Sub Main()
 
  Dim o as Object = Nothing
  MessageBox.Show(o.ToString())
 
End Sub

The (pre)compiler will not report that o is not pointing to an instance. It would only report in this siutation:

VB.NET:
Sub Main()
 
  Dim o as Object
  MessageBox.Show(o.ToString())
 
End Sub

Note the missing = Nothing


So, it is likely that this error isnt being thrown by the programmer's directive, but implicitly by the CLR because the programmer is attempting to use an object that has been initialized or reset to Nothing.

Following the procedure I described for having the CLR break as soon as an exception is thrown rather than unhandled should work for this instance.


We can learn two things from this:
One, it is foolish to write Catch(Exception) if your code block only knows how to handle a specific exception - you should catch that instead. If you must write a generic catch-all then the error message should include the StackTrace, vis:

VB.NET:
Try
  'dodgy operation
Catch(se As SpecificException)
  'deal with specific
Catch(ex As Exception)
  MessageBox.Show("Encountered an error I cant handle: " & ex.StackTrace)
End Try

If the stacktrace were made available to the user, they would see the exact line number of the method that raised the exception (if in debug mode at least. In release mode, line number symbols would be absent but it would still be handy to have this error message). It is important for developers to remember that a stack trace may be tens or hundreds of calls deep, and their try/catch scenarios may be handling errors generated in different modules, maybe even in different projects.

The other is that vital objects should be tested to see that <objectReference> IsNot Nothing before they are used. If they are found to be nothing, then a more specific error can be raised, rather than having to work out which of the hundreds of potential candidates is wasting debugging time
 
Back
Top