0

I build my solution in visual studio(successfull) and I am trying to run it but for some reason at the following line of code it was throwing the exception I went several articles but there was no exact solution how this could be handled

public static int GetCurrentPolicyClassId()
    {
        **int policyClassId = (int) HttpContext.Current.Session[AppConstants.SK_POLICYCLASSID];**
        return policyClassId;
    }
1
  • 1
    Something is null there... put a breakpoint over that line, then you can Watch what's going on with data. Commented Aug 2, 2012 at 14:56

3 Answers 3

3

One of the values in the chain you've called is null. You just need to check before getting the values:

if(HttpContext != null && 
   HttpContext.Current != null &&
   HttpContext.Current.Session != null &&
   HttpContext.Current.Session[AppConstants.SK_POLICYCLASSID] != null)
{
    // Get the value here.
}
else
{
    // Something was null. Either set a default value or throw an Exception
}
Sign up to request clarification or add additional context in comments.

Comments

0

You should probably check if HttpContext != null && HttpContext.Current != null && HttpContext.Current.Session != null

Comments

0

any exception is handled by try/catch (or finally), if that exception is possible to handle in general.

For example StackOverflowException could not be handled.

You need to:

  • what type of exception is
  • understand reasons of it
  • based on this decide if that exception is an exceptional behaviour in your application
  • if yes, handle it with try/catch if program needs to handle it, or leav program to fail, as the exception recieved is too dangerous and it's better to let to fail everything.
  • if this is not exceptional behavior, try to handle it with, for example null checks, or whatever...

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.