I get an error when I do the following:
if(Session['value'] != null) { // code }
The error i get is this:
Object reference not set to an instance of an object.
Why is this? I always check my session this way? I am using the MVC Framework, does this has something to do with it?
EDIT:
The code is in the constructor of a Controller:
public class MyController : ControllerBase { private int mVar; public MyController() { if (Session['value'] != null) { mVar= (int)Session['value']; } } }
The session only really exists during the processing of an action – I wouldn’t expect it to be valid in the constructor of a controller. For example, the controller might (for all I know) be re-used between requests.
You will need to do this either in the action (method), or (perhaps more appropriately) in an action filter, or the
OnActionExecuting(etc) method(s):