casting an object with "as" still giving an error
<p>I have an object MyObject and I'm storing that in the session.</p>

<p>I write this:</p>

<pre><code>MyObject TheObject = HttpContext.Current.Session["TheObjInSession"] as MyObject;
</code></pre>

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

<p>Thanks for your suggestions.</p>

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