Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1981
  • Last Modified:

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

0
Camillia
Asked:
Camillia
3 Solutions
 
WilliamCommented:
test if this is null System.Web.HttpContext
0
 
Anurag ThakurCommented:
try changing the property to
public static int CurrentLocaleId
{
      get
      {
            //return CurrentCulture.LCID;
            if (System.Web.HttpContext.Current.Session["LCID"] == null)
            {
                  System.Web.HttpContext.Current.Session["LCID"] = CurrentCulture.LCID;
            }
            return (int)System.Web.HttpContext.Current.Session["LCID"];
      }
}
0
 
johnaryanCommented:
You should check if the session is null first
if(System.Web.HttpContext.Current.Session == null)
when you System.Web.HttpContext.Current.Session["LCID"], you're checking if the index of LCID is null.

This would throw and error because the underlying object is null.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
alaranCommented:

"System.Web.HttpContext.Current.Session["LCID"] == null" checks if the returned values i NULL. You might get a null reference exception is say System.Web.HttpContext.Current.Session == NULL or System.Web.HttpContext.Current == NULL and so on. Have you tried setting a berak point an just browsing the involved objects in the Locals window? It tends to be of help when I try to solve this kind of thing...
0
 
CamilliaAuthor Commented:
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...
0
 
rambovnCommented:
public static int CurrentLocaleId
        {
            get
            {
                try{
return (int)System.Web.HttpContext.Current.Session["LCID"];
}
 catch (Exception e)
               
                {
                    return CurrentCulture.LCID;
                }
return CurrentCulture.LCID;
               
        }
0
 
alaranCommented:
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 :)
0
 
CamilliaAuthor Commented:
>>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.
0
 
alaranCommented:
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...
0
 
CamilliaAuthor Commented:
you're right..let me dig deeper...
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now