Click here to Skip to main content
15,749,288 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.NullReferenceException: Object reference not set to an instance of an object.
Updated 20-Jan-14 1:59am
Karthik_Mahalingam 20-Jan-14 7:59am    
post your code..
Kornfeld Eliyahu Peter 20-Jan-14 7:59am    
NullReferenceException means that you try to use and object was not initialized prior to it...
BUT! It's impossible to tell more without your code - please post it with the question...
sp_suresh 20-Jan-14 7:59am    
this means the you are passing null to object
Richard4 20-Jan-14 8:06am    
I used Me.Session.Add("con", con)
sometimes it works but sometimes doesn't work.

Is it possible that the session will expire automatically?

<sessionstate mode="InProc" timeout="20">
<customerrors defaultredirect="Login.aspx">
Richard4 20-Jan-14 8:12am    
I decide to replace new session data of users every page load. but still i got same error.
I don't understand, i can't control the error.

1 solution

You can prevent this kind of error to happen by never assuming that an object has been correctly initialized before using it (i.e., always check for a null value before using any class).

In code, here's a small example describing the general idea:
// Here myObject can be any reference-type variable
if (myobject != null) {
   // Do something with myObject
else {
   // Obviously there is a problem, myObject should not be null, but it is.
   // You have to decide here what to do in this case.
Share this answer
Rahul VB 20-Jan-14 12:31pm    
hello sir,
thats correct, very nice. What i used to do was to add a break point on that line and hover the mouse just above that line to see which object is null. Obviously the object is not initialized. Am i correct Sir?
Richard4 20-Jan-14 23:45pm    
In my Class I declare Dim xx As String = Nothing

I used Global.asax to display the error Response.Write("<pre>"& err.InnerException.Message &"</pre>")

I try to put a break point on my code, but i didn't seen Null after executing my code and still Global.asax response "Object reference not set to an instance of an object."

phil.o 21-Jan-14 7:09am    
We would need the complete exception message (along with stack trace) to determine which line of code exactly is throwing the NullReferenceException.

Could you declare:
Dim xx as String = String.Empty
This way your xx variable would not be null (Nothing in VB).
Richard4 22-Jan-14 11:39am    
Thank you for your comments and suggestions.

What i have to do right now is just to put my web app to production, so that i can really know what is the problem.


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