Null reference exception - Query string

  • Thread starter Thread starter Zeba
  • Start date Start date
Z

Zeba

Hi ....

I'm always getting stuck with null reference exceptions !

Can you tell me what's wrong with my code given below ? I'm getting
the error that reference is not set to an instance of an object.


protected void Page_Load(object sender, EventArgs e)
{
String myItem;
if (Request.QueryString["Item"].ToString() != null)
{
myItem = Request.QueryString["Item"].ToString();
Label1.Text = myItem;
}
else
{
Label1.Text = "none";
}
}

Thanks !
 
Zeba said:
I'm always getting stuck with null reference exceptions !

Can you tell me what's wrong with my code given below ? I'm getting
the error that reference is not set to an instance of an object.

On which line? If it's a line with multiple expressions on, have you
tried breaking it down into multiple lines so you can find out exactly
which expression is null?
 
Zeba said:
Hi ....

I'm always getting stuck with null reference exceptions !

Can you tell me what's wrong with my code given below ? I'm getting
the error that reference is not set to an instance of an object.


protected void Page_Load(object sender, EventArgs e)
{
String myItem;
if (Request.QueryString["Item"].ToString() != null)
{
myItem = Request.QueryString["Item"].ToString();
Label1.Text = myItem;
}
else
{
Label1.Text = "none";
}
}

Thanks !

Hi,

Probably Request.QueryString["Item"] evaluates to null.
You better change your code in something like:

protected void Page_Load(object sender, EventArgs e)
{
String myItem;
if(Request.QueryString["Item"] != null)
{
myItem = Request.QueryString["Item"].ToString() ;
Label1.Text = myItem ;
}
else
{
Label1.Text = "none";
}
}

or save some bytes this way:

protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Request.QueryString["Item"] != null ?
Request.QueryString["Item"].ToString() : "none";
}


Regards,
Anne
 
Okayy..! That has solved the problem...So it was ToString() which was
complaining of null reference object, wasn't it..
Thanks !
 
Yes, it was.

Here's an even shorter way of writing the code:

Label1.Text = Request.QueryString["Item"] ?? "none";

:)
Okayy..! That has solved the problem...So it was ToString() which was
complaining of null reference object, wasn't it..
Thanks !
Hi,

Probably Request.QueryString["Item"] evaluates to null.
You better change your code in something like:

protected void Page_Load(object sender, EventArgs e)
{
String myItem;
if(Request.QueryString["Item"] != null)
{
myItem = Request.QueryString["Item"].ToString() ;
Label1.Text = myItem ;
}
else
{
Label1.Text = "none";
}

}

or save some bytes this way:

protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Request.QueryString["Item"] != null ?
Request.QueryString["Item"].ToString() : "none";

}

Regards,
Anne
 
Hi,

Zeba said:
Hi ....

I'm always getting stuck with null reference exceptions !

Can you tell me what's wrong with my code given below ? I'm getting
the error that reference is not set to an instance of an object.


protected void Page_Load(object sender, EventArgs e)
{
String myItem;
if (Request.QueryString["Item"].ToString() != null)

This may be the line, look into the docs what happens if "Item" does not
exist in the collection
 
Yes, it was.

Here's an even shorter way of writing the code:

Label1.Text = Request.QueryString["Item"] ?? "none";

:)

What, this works? Would be nice... :) I thought in C#, we have to
explicit compares to null?

Quoc Linh
 
Quoc Linh said:
Yes, it was.
Here's an even shorter way of writing the code:
Label1.Text = Request.QueryString["Item"] ?? "none";
:)
What, this works? Would be nice... :) I thought in C#, we have to
explicit compares to null?
?? is the null coalescing operator.

I did not know that. Very useful tip. Thanks Jon!

Oh, and thanks Goran for bringing up the original code ! :)

Quoc Linh
 
Hey that article was really interesting. Earlier I hadn't understood
how Goran's code did its work.

Thanks !
 
Hey that article was really interesting. Earlier I hadn't understood
how Goran's code did its work.

Thanks all of ya!
 
Hey that article was really interesting. Earlier I hadn't understood
how Goran's code did its work.

Thanks all of ya!
 
Back
Top