Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

Why do I get Object Null reference here

I have the code below. First time page loads, the "session" is null so it goes down the right path.
Page loads. I click a menu item. Same piece of code is called but this time, i get a null reference ..but i am checking for: if (....=null)


public static int CurrentLocaleId
        {
            get
            {
                //return CurrentCulture.LCID;
               
 
                if (System.Web.HttpContext.Current.Session["LCID"] == null)
                {
                    return CurrentCulture.LCID;
                }
                else
                {
                    return (int)System.Web.HttpContext.Current.Session["LCID"];
                }
               
            }
        }

Open in new window

Avatar of William Domenz
William Domenz
Flag of United States of America image

test if this is null System.Web.HttpContext
SOLUTION
Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Camillia

ASKER

I have set breakpoints and this is where that property gets called which throws the error:

category = Category.GetByPath(categoryPath, CXEK.Locales.CurrentLocaleId);

but let me try the suggestions...
public static int CurrentLocaleId
        {
            get
            {
                try{
return (int)System.Web.HttpContext.Current.Session["LCID"];
}
 catch (Exception e)
               
                {
                    return CurrentCulture.LCID;
                }
return CurrentCulture.LCID;
               
        }
Rambovn: If you do that last thing often, you should stop.
Everyone: Do not use exception handling to control program flow. An if you do something like the above, at the very least catch NullReferenceException in stead of Exception.
In stead check to see what might be null and handle it by checking for it.

Farzadw:
Does the exception get thrown in this line:
category = Category.GetByPath(categoryPath, CXEK.Locales.CurrentLocaleId);

...or does it get thrown in the property code?

Any ways, step through the code, and use the locals window to check what is null and what is not. Just don't opt for the simple try catch way out of it if you can avoid it :)
>>Does the exception get thrown in this line:
category = Category.GetByPath(categoryPath, CXEK.Locales.CurrentLocaleId);
...or does it get thrown in the property code?

In the property code...This is what i dont undestand. I set the value in session..howcome it gets there and it's null...maybe gets reset somewhere...not sure. Have to step thru it again.
The question is mostly "what is actually null?", it is evidently not the value returned by System.Web.HttpContext.Current.Session["LCID"] . If it was the if statement should not thrown an exception. So eiter Session or Current or something further down the line i null... So it's a question of what...
you're right..let me dig deeper...