473,387 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 24202
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
7
by: odonel | last post by:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: here is my code: HttpCookie cookie = Request.Cookies;
0
by: muralidharan | last post by:
WebForm1.aspx Code: <%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %> <ComponentArt:TreeView id="TreeView1" Height="520"...
2
by: I am Sam | last post by:
I keep getting the following exception error: Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request....
0
by: webbsk | last post by:
I keep getting an exception when I call the DataAdapter Update method. I have been trying to figure out what is causing the null reference exception for this code for what seems like forever: ...
2
by: Simon Rigby | last post by:
Hi folks, A bizarre problem I am having. I have a treeview which is bound to an XmlDataSource. The XMLDataSource.Data property is set to the result of a function that generates an XML...
1
by: connor7777 | last post by:
Hi guys: We've been weeding out errors off of a java->c# project and have managed to redeem most of our code with the exception of one bug that we for some reason cannot pin down. The following...
2
by: Manikandan | last post by:
Hi, I have a table with following data Tablename:details No(varchar) Name(varchar) Updated(Datetime) 1 mm 10/10/2006 2 nn 02/12/2005 3 kk NULL I'm using executescalar to get the...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.