Solved

Why do I get Object Null reference here

Posted on 2008-09-30
10
1,973 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
ID: 22606833
test if this is null System.Web.HttpContext
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 100 total points
ID: 22606839
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
ID: 22606840
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 4

Assisted Solution

by:alaran
alaran earned 200 total points
ID: 22606883

"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
ID: 22607692
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
 
LVL 8

Expert Comment

by:rambovn
ID: 22607902
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
ID: 22613025
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
ID: 22614016
>>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
ID: 22614224
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
ID: 22614339
you're right..let me dig deeper...
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

786 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