Solved

Why do I get Object Null reference here

Posted on 2008-09-30
10
1,968 Views
Last Modified: 2012-06-21
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
Comment
Question by:Camillia
10 Comments
 
LVL 11

Expert Comment

by:William
Comment Utility
test if this is null System.Web.HttpContext
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 100 total points
Comment Utility
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
 
LVL 5

Accepted Solution

by:
johnaryan earned 200 total points
Comment Utility
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
 
LVL 4

Assisted Solution

by:alaran
alaran earned 200 total points
Comment Utility

"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
 
LVL 7

Author Comment

by:Camillia
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

by:rambovn
Comment Utility
public static int CurrentLocaleId
        {
            get
            {
                try{
return (int)System.Web.HttpContext.Current.Session["LCID"];
}
 catch (Exception e)
               
                {
                    return CurrentCulture.LCID;
                }
return CurrentCulture.LCID;
               
        }
0
 
LVL 4

Expert Comment

by:alaran
Comment Utility
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
 
LVL 7

Author Comment

by:Camillia
Comment Utility
>>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
 
LVL 4

Expert Comment

by:alaran
Comment Utility
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
 
LVL 7

Author Comment

by:Camillia
Comment Utility
you're right..let me dig deeper...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now