2

Why is this code throwing a NullReferenceException?

Exception:

System.NullReferenceException: Object reference not set to an instance of an object.

Code:

if ((string.IsNullOrEmpty(Request.QueryString["Sno"].ToString())) 
   && (string.IsNullOrEmpty(Request.QueryString["Name"].ToString())))
{
    lblBookedBy.Text = "";
    lblSno.Text = "";
}
else
{
    lblBookedBy.Text = Request.QueryString["Name"].ToString();
    lblSno.Text = Request.QueryString["Sno"].ToString();
}
2
  • have you checked my answer ? Commented Mar 15, 2013 at 10:59
  • 1
    ans rest of the people answered here are out of their mind right????? Commented Mar 15, 2013 at 11:15

6 Answers 6

2

I would recommend doing the following.

if (Request.QueryString["Sno"] == null || Request.QueryString["Name"] == null)
{
    lblBookedBy.Text = "";
    lblSno.Text = "";
}
else
{
    lblBookedBy.Text = Request.QueryString["Name"].ToString();
    lblSno.Text = Request.QueryString["Sno"].ToString();
}

You are most likely getting a NullReference in the if statement. This way you are sure not to encounter this, and worst case if both of the variables are instantiated, but one or more contains an empty string it will simply set the Text to empty.

Alternatively if you use Convert.ToString as many other suggested you can simplify the code by skipping the if statement.

lblBookedBy.Text = Convert.ToString(Request.QueryString["Name"]);
lblSno.Text = Convert.ToString(Request.QueryString["Sno"]);

In worst case scenario one of these will be Null, and will result in one of the TextBoxes to show result, while the other one is empty.

Also, assuming that Request.QueryString supports it, you could use TryGetValue.

Sign up to request clarification or add additional context in comments.

3 Comments

it will throw an exception if ["Sno"] and ["Name"] will be null.
I don't see how either one of them could be null. I am not doing a ToString in the if statement.
The edit was rejected, but your solution works as well, but is not needed in my answer as it's not possible for the values to be Null.
1

You are trying to cast Request.QueryString["Sno"] to string while it's value is null

Comments

1

This is most likely because there is no key "Sno" or "Name" in the collection query string which results in the exception when trying to invoke .ToString() on it.

Check if Request.QueryString["Sno"] or Request.QueryString["Name"] is null, before invoking .ToString()

3 Comments

yes I know they are null but after this the else part should run. Why error ?
@Gaurav can you convert null to string?
Your if statement checks that they are both null (or empty) If only one of them is null, you'll see this error. Use an or in your if test instead.
1

if Request.QueryString["Sno"] or Request.QueryString["Name"] does not return any object than calling ToString will throw exception

Comments

1

Remove .ToString() in null check because when query string parameter is not available you are trying to type cast null variable to string....

if ((string.IsNullOrEmpty(Convert.ToString(Request.QueryString["Sno"]))) && (string.IsNullOrEmpty(Covert.ToString(Request.QueryString["Name"]))))

Comments

-1

Use Convert.ToString() instead of .ToString() to avoid null reference exception:

if (string.IsNullOrEmpty(Convert.ToString(Request.QueryString["Sno"])) && string.IsNullOrEmpty(Covert.ToString(Request.QueryString["Name"])))
{
    lblBookedBy.Text = "";
    lblSno.Text = "";
}
else
{
    lblBookedBy.Text =Convert.ToString(Request.QueryString["Name"]);
    lblSno.Text =Convert.ToString(Request.QueryString["Sno"]);
}

1 Comment

Did you rollback my change and do the exact same edit yourself? I wasn't the one that rejected the changes on my post. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.