469,338 Members | 8,430 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

Null reference exception - Query string

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 !

Feb 23 '07 #1
12 24027
Zeba <co******@gmail.comwrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 23 '07 #2

"Zeba" <co******@gmail.comschreef in bericht
news:11**********************@q2g2000cwa.googlegro ups.com...
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
Feb 23 '07 #3
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

Feb 23 '07 #4
Yes, it was.

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

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

:)

Zeba wrote:
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


--
Göran Andersson
_____
http://www.guffa.com
Feb 23 '07 #5
Hi,

"Zeba" <co******@gmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
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

Feb 24 '07 #6
On Feb 23, 4:50 am, Göran Andersson <g...@guffa.comwrote:
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

Mar 1 '07 #7
Quoc Linh <le********@yahoo.comwrote:
On Feb 23, 4:50 am, Göran Andersson <g...@guffa.comwrote:
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.

See http://pobox.com/~skeet/csharp/csharp2/nullable.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 1 '07 #8
On Mar 1, 12:22 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
Quoc Linh <lequocl...@yahoo.comwrote:
On Feb 23, 4:50 am, Göran Andersson <g...@guffa.comwrote:
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

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
I did not know that. Very useful tip. Thanks Jon!

Mar 1 '07 #9
On Mar 1, 1:52 pm, "Quoc Linh" <lequocl...@yahoo.comwrote:
On Mar 1, 12:22 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
Quoc Linh <lequocl...@yahoo.comwrote:
On Feb 23, 4:50 am, Göran Andersson <g...@guffa.comwrote:
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
--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www..msmvps.com/jon.skeet
If replying to the group, please do not mail me too

I did not know that. Very useful tip. Thanks Jon!
Oh, and thanks Goran for bringing up the original code ! :)

Quoc Linh

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

Thanks !

?? is the null coalescing operator.

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

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

Thanks all of ya!

?? is the null coalescing operator.

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

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

Thanks all of ya!

?? is the null coalescing operator.

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

Mar 2 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by David Sworder | last post: by
7 posts views Thread by odonel | last post: by
reply views Thread by muralidharan | last post: by
2 posts views Thread by I am Sam | last post: by
2 posts views Thread by Simon Rigby | last post: by
2 posts views Thread by Manikandan | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.