Solved

Why do I get Object Null reference here

Posted on 2008-09-30
10
1,970 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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, f…

863 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

27 Experts available now in Live!

Get 1:1 Help Now