Null reference exception - Query string

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 !
 
J

Jon Skeet [C# MVP]

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?
 
A

AvdP

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
 
Z

Zeba

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

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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
 
Q

Quoc Linh

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
 
Q

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.

Seehttp://pobox.com/~skeet/csharp/csharp2/nullable.html

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

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
 
Z

Zeba

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

Thanks !
 
Z

Zeba

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

Thanks all of ya!
 
Z

Zeba

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

Thanks all of ya!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top