Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello all,
i am new to learning asp.net and c#

I am using Session["variables"] i my project to store user details for some time
but sometime while checking in the page_load like
C#
if (string.IsNullOrEmpty(Session["Username"] as string))
{
     //Some Stuff to initialize the variable
}


I get error
C#
System.NullReferenceException: Object reference not set to an instance of an object.


please tell me

Q1.how do i catch these error and ignore it OR initialize it and proceed?
or
Q2.How can i initialize Session["variables"] every time when website loads into client machine so that i can initialize it and don't get error?

please help
thanks

What I have tried:

tried storing data in as global variables but it is not working perfectly.
Posted
Updated 8-Feb-17 7:46am
v4
Comments
Richard Deeming 8-Feb-17 13:33pm    
You need to start by working out which object is null.

Based on the code you've posted, you would only get a NullReferenceException if Session is null. But the Page.Session property will never return null - if session state is disabled for the page, it would throw an HttpException instead.

So it seems highly unlikely that the line of code you've posted is the source of the exception. Double-check the stack trace of the exception, and also check the surrounding lines in case the line number is wrong.
Palash Sachan 8-Feb-17 13:41pm    
i know which session objects are NULL when the page loads and so i am checking it that if its null then do some stuff..but it gives error
do u know how can i initialize the session variable when the website loads on client machine?
Richard Deeming 8-Feb-17 13:58pm    
As I said, the code you've posted will not generate a NullReferenceException. You might get an HttpException if session state is disabled, but you won't get a NullReferenceException.

(There was a bug back in .NET 2.0 RTM where string.IsNullOrEmpty would throw a NullReferenceException; but that was fixed in SP1, which was released in 2011.)

As I said, the code you've shown is not responsible for the exception you're seeing. You need to track down the code that is responsible, and fix that.
Palash Sachan 8-Feb-17 14:04pm    
yeah but its showing error..i don't know why its showing error when everything is fine..anyway thanks for the help :)
Richard Deeming 8-Feb-17 14:07pm    
As I said, check the stack trace of the exception. That should tell you what line in what file caused the exception. With ASP.NET, the line number can sometimes be a couple of lines off, so check the surrounding lines as well.

If you can reproduce the error, debug your code. Visual Studio will break on the line where the error occurred, and you will be able to inspect the variables to see what's null when it shouldn't be.

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