Solved

Why do I get Object Null reference here

Posted on 2008-09-30
10
1,974 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Simple Injector with Web Service 4 55
Why does my excel data start at B1 instead of A1. 9 93
Hey Audio! 9 45
Can I command line build a C# application? 6 41
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

821 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