0

What is wrong with my code? I recently posted a question about Calculation in code behind and I tried Vinoth's answer but it gives me an error at this line:

bool isChaffeurUsed = (bool)Session["IsChaffeurUsed"];

error message is: Object reference not set to an instance of an object.

Please tell me what should i do. Many thanks and have a nice day.

Community
  • 1
  • 1
Stuck
  • 229
  • 1
  • 3
  • 11
  • 1
    All answers below are correct but you should also check that the value is a boolean http://msdn.microsoft.com/en-us/library/system.boolean.tryparse.aspx – Amiram Korach Aug 01 '12 at 13:53

7 Answers7

4
Session["IsChaffeurUsed"]

Is not defined - you haven't set any session variable with the key IsChaffeurUsed

You need to check if it's set first,

bool isChaffeurUsed;

if(Session["IsChaffeurUsed"] != null)
    isChaffeurUsed = (bool)Session["IsChaffeurUsed"];
Shai
  • 25,159
  • 9
  • 44
  • 67
3

You need to check the object first, try:

var isChaffeurUsed = false;

if (Session["IsChaffeurUsed"] != null)
{
    isChaffeurUsed  = bool.Parse(Session["isChaffeurUsed"].ToString());
}
Jupaol
  • 21,107
  • 8
  • 68
  • 100
3

The error is trying to tell you that Session["IsChaffeurUsed"] doesn't exist.

If you know a default value, you could change the statement to read:

bool isChaffeurUsed = (bool)(Session["IsChaffeurUsed"] ?? false)

Or, if you want to allow null values (which would indicate that the value wasn't set specifically to any value), you could use a nullable type:

bool? isChaffeurUsed = (bool?)Session["IsChaffeurUsed"];
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
1

Most likely, you don't have anything in Session with the name "IsChaffeurUsed".

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
1

You would get that exception if Session was null or if IsChaffeurUsed was not found in Session. Session is probably not null, so the problem is likely that IsChaffeurUsed is not found.

You need to decide what to do if the IsChaffeurUsed was not set. For example, you could assume it's false:

bool isChaffeurUsed = Session["IsChaffeurUsed"] == null ? false 
     : (bool)Session["IsChaffeurUsed"];
Matt Varblow
  • 7,651
  • 3
  • 34
  • 44
0

TryParse() methods exist for this reason:

bool.TryParse(Session["IsChaffeurUsed"], out isChaffeurUsed)
Kyle B.
  • 5,737
  • 6
  • 39
  • 57
  • You'd use TryParse if it was a string. It sounds like he's expecting it to be a boolean. Session[X] returns an object that he's casting to a boolean. There's no reason to do string parsing in this case. – Matt Varblow Aug 01 '12 at 15:11
  • Do you feel it's safe to infer/assume type? Should this not be of type bool, attempting to cast it as such would produce an unhandled exception. – Kyle B. Aug 01 '12 at 15:14
  • I would typically have one piece of code that reads/writes the value from the session, usually a property with a getter and setter. In that case I assume it's safe. If someone puts an inproper value in the session with the same key then I want it to throw an exception. If I did want to be more forgiving about the type then I'd use Convert.ToBoolean rather than bool.TryParse. – Matt Varblow Aug 02 '12 at 14:15
  • Not trying to nit-pick, but Convert.ToBoolean will throw a System.FormatException if it contained a value which could not be converted (i.e. "test-value-123"). I still feel this is a more effective approach since Session cannot be guarded in all cases. – Kyle B. Aug 02 '12 at 19:28
  • But don't you want it to throw an exception if the state is completely unexpected and incorrect? I suppose it depends on the context. – Matt Varblow Aug 03 '12 at 14:08
  • I believe it would be better to use the boolean returned from the TryParse method which indicates if it was able to successfully retrieve a valid value and act accordingly if not, but again, just my own opinion. – Kyle B. Aug 03 '12 at 15:11
  • And I think it would be better to always access that session variable using a custom property with a getter/setter. The getter would simply check for null and cast, throwing an exception if the value was set improperly. Just my opinion :) – Matt Varblow Aug 03 '12 at 16:51
0

One thing about looking at session variables is that there's a possibility the variable will be gone after the initial read (this has happened to me on a few occasions). This is usually the pattern I use when dealing with looking at session/cache variables in an ASP app:

object o = null;
if((o = Session["IsChaffeurUsed"]) != null)
{
    // Do something with o: bool.Parse, (bool), etc...
}
SPFiredrake
  • 3,852
  • 18
  • 26