Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I get the following exception and don't know in what line it occurs so that i resolve it:
A first chance exception of type 'System.NullReferenceException' occurred in UserControls.dll

What I have tried:

I need to know what line in code causes the message to be shown in the Output window and I've no clue.
Posted
Updated 6-Oct-18 5:59am
Comments
F-ES Sitecore 6-Oct-18 9:14am    
Add exception handling and logging to the methods and capture the stack trace. You get than code when you try

someObject.SomeProperty

where "someObject" is null.
ilostmyid2 6-Oct-18 9:27am    
sure, but don't know where it occurs in a solution with lots of projects. so that i don't know where to put the try/catch block. this is the problem.
Eric Lynch 6-Oct-18 10:13am    
Simply add a "last chance" try/catch block. Where exactly, depends on the type of application. For most types, you can place it around the logic in the Main method. For web apps, its a bit different, but same concept.

Or, you could simply run it in the debugger, which will normally break at the offending line, when an exception occurs.
ilostmyid2 7-Oct-18 4:04am    
i think there's a problem. the exception is thrown somewhere in the code, but it's catched there, without being handled. so i can't find it. the exception is muted this way.

The simplest way is to just use the debugger - when it hits an unhandled exception, it will stop and show you exactly what it was executing - you can then use the stack trace to work back up to your code (if it occurs in the framework for whatever reason).

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
ilostmyid2 7-Oct-18 4:06am    
i had read it in the internet, but as i said to Eric Lynch, the exception is muted and i can't find it.
OriginalGriff 7-Oct-18 4:36am    
If you are getting an exception then it isn't "muted" whatever that is supposed to mean. And running it in the debugger will stop you code at the line the exception happens on. If that is in system code, then either it will load the system code from the reference sources, or it will show the last line of your code that called the system code.

You can also make it even more obvious: in Visual Studio, go to the Debug menu, select "Exceptions..." and tick every box in there before pressing "OK". Now the debugger will stop for every exception, even if you have a handler for it.

We can't do this for you: you need your code running in the debugger to find out what is happening, and we don't have access to it and it's data!
ilostmyid2 8-Oct-18 5:28am    
thanks for the trick, i'm going to test it.
but i don't get an exception, this has been the problem. it's just reported in the output window and the code keeps running as there has been no exception.
OriginalGriff 8-Oct-18 5:31am    
If it's reported in the output window, then it's being caught and either Debug.WriteLine or Console.WriteLine is being used to show you - but ticking all the boxes should mean that VS stops at it.
ilostmyid2 8-Oct-18 5:34am    
indeed i was looking for this. the exception had been muted by the Debug menu. please put it as a solution so that i may accept it. thx
C#
try
{
   // code that may raise an exception
}
catch (NullReferenceException ex)
{
   msgbox($"{ex.Message} {ex.StackTrace}")
}



The ex.StackTrace will show where the error occurred (inc. line no)
 
Share this answer
 
Comments
ilostmyid2 7-Oct-18 4:07am    
the problem is that i can't find the code block which raises the exception, because it's being muted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900