Great India Shopping

Tuesday, March 8, 2011

Error:Object Reference Not Found in .Net

Hi,
This is one error which every .Net developer might have faced at some point of his coding life. I provide 1 reason/ scenario when this error would occur.
Example :
1) when your session object becomes null and you have tried to assign it to another variable.
session["username"] = null ;
string myusername;
myusername = session["username"].ToString();
Here in the last statement when CLR tries to get a string value of a session object , no value is found in it. So it throws an "Object Reference Not Found" error.
Solution : Check your session objects for null values.
This is 1 scenario I encountered.I will keep hitting this post when I face other scenarios where the same error occurs.
Happy Coding !!

No comments:

Post a Comment