I have one line of code that throws an exception, and when the catch(Exception ex) line executes, ex is NULL. Am I interpreting this correctly? If this is happening, does anyone have an idea of how this could occur? I am using a lot of Telerik RAD controls, could that be causing an issue?

In the Watch window this information displays:
Name: ex
Value: Object reference not set to an instance of an object

`try
{
    RadScriptManager sm = new RadScriptManager();
    sm.ID = "ScriptManager1";
}
catch(Exception ex)
{
    System.Console.WriteLine("Error: "+ex.Message);
}`

The exception is thrown when I access sm to set its ID.

Dani AI

Generated

Short answer: the exception variable is almost certainly not null. The debugger display of "Object reference not set to an instance of an object" is the NullReferenceException's message, and a breakpoint placed on the catch line can stop execution before the catch parameter is initialized. As hinted, breakpoints on the catch header can make the variable look uninitialized; as pointed out, that text is the exception message explaining why the throw occurred, not proof that ex itself is null.

Practical checks and a safer catch pattern:

  • Put the breakpoint on the first statement inside the catch block (not on the catch(...) line) and, in the Immediate window, evaluate ex == null, ex.GetType().FullName and ex.StackTrace to see the actual type and origin.
  • To preserve and log full details, use a catch that writes type, message and stack trace and then rethrows (so the original stack is preserved):
catch (Exception ex)
{
    System.Diagnostics.Debug.WriteLine("Type: " + ex.GetType().FullName);
    System.Diagnostics.Debug.WriteLine("Message: " + ex.Message);
    System.Diagnostics.Debug.WriteLine("Stack: " + ex.StackTrace);
    throw;
}

If a third‑party control (like Telerik's RadScriptManager) is involved, note that many server controls assume a hosting ASP.NET context (Page, NamingContainer, HttpContext). Instantiating or changing control properties outside the normal page lifecycle can cause an internal null dereference. Enable break‑on‑throw for CLR exceptions (Debug → Exception Settings) so the debugger stops at the original throw site; the stack trace will show which member was dereferenced and where to fix it.

Recommended Answers

All 3 Replies

Is your break point ON the catch(exception ex) line or past it?

If your break point is ON the catch line the 'ex' variable has not been initiated or assigned yet. Try putting your break point IN your catch block.

If this is not the case could you supply some code? Object regerence not set to an instance of an object means you are trying to do work with data that is null. Without seeing your code it's very difficult to help.

My breakpoint is at line 7. It breaks and then I step one line in (line 8) and then look at the value of ex. That's where I see the null reference type of message.

"Object reference not set to an instance of an object" is the message contained by the Exception, it does not refer to the exception itself. It's telling you that that is the reason the particular line of code threw the exception.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.