2

I have an object MyObject and I'm storing that in the session.

I write this:

MyObject TheObject = HttpContext.Current.Session["TheObjInSession"] as MyObject;

I'm getting error Object reference not set to an instance of an object Why? I thought that if you use as to cast and the cast produces an error then it just returns null and execution continues. How should I rewrite this line?

Thanks for your suggestions.

PS: The error is produced because I removed the enable session line [WebMethod(EnableSession = true)] I'll put the line back in but I want to make sure there are no exceptions in case anything goes wrong.

frenchie
  • 51,731
  • 109
  • 304
  • 510

1 Answers1

4

Chances are the Session is triggering it (it either doesn't exist or hasn't been established). This is common in handlers if you haven't marked the class to manage sessions.

Yes, you're right, as will attempt a cast or result in a null, but that's only assuming you're left-hand object is valid and has a value to attempt a cast on.

To explain it a bit more in depth:

HttpContext.Current.Session["Foo"]
HttpContext                         Probably fine
HttpContext.Current                 May not exist
HttpContext.Current.Session         Most likely cause (no access to session)

Which essentially makes referencing a key off the Session element impossible if Session itself doesn't exist.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • The error is produced because I removed the enable session line [WebMethod(EnableSession = true)] . Of course, I'll put the line back but why is it that the as opertor throws an error? – frenchie Jul 26 '12 at 17:46
  • @frenchie: If you replace the `WebMethodAttribute` and allow the handler access you should be fine. Although I'm a firm believer in practice makes perfect; or basically "try and see for yourself". Also, given this is a webservice you should be fine just referencing the property `Session` instead of trying to access HttpContext's static member. – Brad Christie Jul 26 '12 at 17:48
  • I'm looking to make the code safe if the session IS null or not working. In this case, there's a branch of code that exits the webservice "nicely". Bu right now, if the cast fails, it'll just throw an exception. – frenchie Jul 26 '12 at 17:50
  • What do you mean by "but that's only assuming you're left-hand object is valid and has a value to attempt a cast on." – frenchie Jul 26 '12 at 17:51
  • @frenchie: Have a look at [this other SO question/answer](http://stackoverflow.com/questions/1382791/asp-net-what-to-do-if-current-session-is-null) with regards to handling a null session. And what I mean is `as` only works when it can retrieve a value first. If it fails getting the value (i.e. `Session` is null and therefor `Session[key]` won't work) it's moot about how effective the `as` cast is. Worded differently, `as` isn't the problem (as originally implied by your question) it's `Session` that's the problem. – Brad Christie Jul 26 '12 at 17:53
  • Ah ok, got it: if it can't access .Current.Session there's no casting at all occurring because there's nothing to access in the first place. Thanks. – frenchie Jul 26 '12 at 18:00
  • I think you meant "but that's only assuming you're RIGHT-hand object is valid." – frenchie Jul 27 '12 at 03:25
  • @frenchie: I was using left/right in terms of the `as`, but you're right--everything past the `=` is the right-hand. – Brad Christie Jul 27 '12 at 12:59