System.NullReferenceException Object reference not set to an instance of an object.
This error occurs in some specific .aspx pages,but iregularly.
Can you suggest some arounds for it and what are the causes for it??
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Akhil MittalPosted Aug 2, 2013, 11:10 AM
Iftikar HussainPosted Aug 2, 2013, 8:11 AM
Can you share that particular aspx code where this error is occurred?
Regards,
Iftikar
Scott LyslePosted Aug 2, 2013, 8:09 AM
For example, if I declare SomeClass myObject; and then I do something to it like myObject.GetDimension(); Since I never created the object (e.g., SomeClass myObject = new SomeClass();) the code will throw the exception.
Another example is in cases where you might populate an object using a method that might not always create an instance of that object. For example, SomeClass myObject = SomeClass.PopulateMyObject(); If PopulateMyObject does not always set the object to an instance of an object and then you try to do something with that object (say, myObject.ToString();, it will throw the exception.
The easiest way around seeing this error is to check to see if the object you tried to create is null before you attempt to perform any operations on it:
if(myObject != null)
{
// then do what you need to do with the object
}
else
{
// treat is like an error or do something else that will not throw the error
}
Sunny SharmaPosted Aug 2, 2013, 8:05 AM
This is being caused by a server side operation.
You're trying to assign a variable with a null value. if it's happening some times then there may be some db operations or it completely depends on your logic laid out by you. Please share your code for more help.