Click here to Skip to main content
15,883,925 members
Please Sign up or sign in to vote.
1.70/5 (3 votes)
See more:
Hi,

I am getting the error "object reference not set to an instance of an object" while i run this piece of code, let me know what can be done to get rid of this.


C#
string cntry = Session["ddlCountry"].ToString();

            if (cntry == "US")
            {
                lnkUrl.Text = string.Empty;
                if (lnkUrl == null ||  lnkUrl.Text.Length == 0)
                {
                    lnkUrl.Text = "http://www.google.com";
                }
Posted
Updated 11-Mar-17 0:02am
Comments
Richard MacCutchan 29-Nov-13 5:07am    
Make sure every reference actually points to something. Also in future please show the exact line of code that gives the error.

This is basically due to the Session object.
This is because the session is not having value i guess.
So do one thing before accessing the Session value check whether the session is exists or not. If exists then only assign this session value to variable.


Example:
Put this code in the page_Load method.First comment the Session initialization and check later on un comment and check this. Hope you will get the answer what you are searching.


C#
Session["ddlCountry"] = "US";

if (Session["ddlCountry"]!=null)
{
    string cntry = Session["ddlCountry"].ToString();

    if (cntry == "US")
    {
        lnkUrl.Text = string.Empty;
        if (lnkUrl == null || lnkUrl.Text.Length == 0)
        {
            lnkUrl.Text = "http://www.google.com";
        }
    }
}
else
{
    lnkUrl.Text="Value not present in Session.";
}



Best of luck.
 
Share this answer
 
v2
Comments
OriginalGriff 29-Nov-13 5:24am    
Reasons for my vote of one: No, it isn't.
Try this:
string s = null;
if (s == "US")
{
}
It will not throw an exception at all - so it doesn't matter if the session return value is null, it will not cause his problem
C@dER@j 29-Nov-13 5:35am    
Your point is correct the code which you have written wont through ant exception. But in order to convert the null value to string using the
"string cntry = Session["ddlCountry"].ToString();"
this code throws exception. If you dint get my comment then check the difference between the function .ToString() and Convert.ToString("") then you will get to know.
Dmytro Bogachev 29-Nov-13 5:39am    
Let me disagree with you. My answer is similar in Session part. And null-value Session object might be one of reasons why there is such problem. You CAN compare to null, as in your case, but here is what happens in original code:
string s = null; //Session["whatever"] is null
s.ToString(); // And exception
C@dER@j 29-Nov-13 5:50am    
This is what i told.if you have null and you are going to convert to string using .tostring() then it will arise an exception. So if you are checking that null value if its not there then only you are converting using .ToString()..
OriginalGriff 29-Nov-13 5:39am    
Ignore the above - I was making a coffee and realised that I had missed the "ToString" on the session access - which makes you right and me way wrong! Sorry...:O
I have corrected the vote to 5, and can only blame a Senior Moment combined with a lack of caffeine. And natural born stupidity, of course.
Why are you using a property of an object, and then later checking it for null?
C#
lnkUrl.Text = string.Empty;
 if (lnkUrl == null ||  lnkUrl.Text.Length == 0)
The second text is completely useless anyway, because the previous line indirectly sets the Length to zero anyway!
Swap it over:
C#
if (lnkUrl != null)
   {
   lnkurl.Text = "http://www.google.com";
   }
And don't try to set properties of null values!
 
Share this answer
 
Comments
Rock_Dead 29-Nov-13 5:17am    
I want to set the LinkURL text as http://www.google.com"; do u think this will solve the purpose?
OriginalGriff 29-Nov-13 5:27am    
You can only set the value of it's Text property if lnkURL is not null - i.e. if it has been set to an instance of an object somewhere.
If the code is throwing a null reference exception when you try to access the Text property, then you need to look at the rest of your code to find out why the value has not been set yet. I can't, because you do not show that code in your fragment.
So, use the debugger to "backtrack" through your code and work out how you got there!
Rock_Dead 29-Nov-13 5:40am    
I tried with this code if (lnkUrl != null)
{
lnkurl.Text = "http://www.google.com";
}
but still throws the same error
Rock_Dead 29-Nov-13 5:52am    
My Link URL is null now and i want to populate the same with some text
OriginalGriff 29-Nov-13 5:40am    
Ignore me - it's not that at all - it's your session as has been said already - I missed the ToString on you session access which will be throwing the exception if the session value has not been set - so look at why that is null (and check it before you try to do anything with it!)
Check if you Session["ddlCountry"] has value before working with it. Check, if your lnkUrl is set to valid control.
So code might be like this:
C#
if(Session["ddlCountry"] != null)
{
    var cntry = Session["ddlCountry"].ToString();
    if (cntry.Equals("US"))
    {
        //You cannot use this control, if lnkUrl is null
        //Since you're  setting lngUrl.Text = string.Empty, it will have 
        //Text.Length == 0  in all cases; so consider changing your logic. 
        //Probably like in code below.
        if(lnkUrl != null)
        {
            if(lnkUrl.Text.Length == 0)
            {
                lnkUrl.Text = "http://www.google.com";
            }
        }
    }
}
 
Share this answer
 
v3
Comments
Rock_Dead 29-Nov-13 5:49am    
getting the same error with this code even
Dmytro Bogachev 29-Nov-13 5:53am    
Did you try Debug? What is exact line of your exception? There are 2 possible points of failure in above code, Session["ddlCountry"].ToString() - if ToString() for some reason returns null (and I don't see reason for this behavior) and if lnkUrl.Text is null - this is also weird.
Rock_Dead 29-Nov-13 5:58am    
i debugged it is the same exception in line lnkUrl.Text = "http://www.google.com";
and also i am changing this line i.e. if(lnkUrl != null) to if(lnkUrl == null) as the lnkUrl is coming as null

Dmytro Bogachev 29-Nov-13 6:00am    
So your problem is not in above code, it is correct. Show please code, where you're getting lnkUrl. Basically it's about this control.
Rock_Dead 29-Nov-13 6:01am    
I have not set text value in this Linkurl. this is the code

<asp:LinkButton ID="lnkUrl" runat="server" CssClass="linkurl" ForeColor="#006600"
Font-Bold="True" Font-Names="Verdana" Font-Size="8pt">
Before reading Session, always check for null.
C#
if(Session["ddlCountry"] != null)
{
    string cntry = Session["ddlCountry"].ToString();
}

If Session ddlCountry value is not present, it will throw "Object reference exception".
 
Share this answer
 
Comments
N.Manjula 13-Feb-16 5:38am    
I have Copied the code but I get an error Invalid MAshape key even i have pasted testing key
I am not getting. What are you trying to do?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900