Problem checking the lenght of a string

V

vbMark

My code in the convert.aspx.cs page:

private void Page_Load(object sender, System.EventArgs e)
{
string sFile = Request.QueryString["File"];

if (sFile.Length > 0) <--- Error here.
{
Response.Write(sFile);
}
else
{
Response.Write("No file");
}
}

Causes

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

If I take out the "if" statement and just put

Response.Write(sFile);

Then it works fine. What is causing this error?

Thanks!
 
S

S. Justin Gengo

vbMark,

If the request contains nothing at all then the string sFile is Nothing.

Checking the length on a string object = Nothing causes this error. You
either need to check if the string is not nothing and then do your length
check, or just check that it isn't nothing and skip the length check all
together.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
G

Guest

I would guess that the value that you are searching for in the QueryString is null. Check your spelling/case.
 
V

vbMark

Yeah, it's null. Thanks.

I would guess that the value that you are searching for in the
QueryString is null. Check your spelling/case.

vbMark said:
My code in the convert.aspx.cs page:

private void Page_Load(object sender, System.EventArgs e)
{
string sFile = Request.QueryString["File"];

if (sFile.Length > 0) <--- Error here.
{
Response.Write(sFile);
}
else
{
Response.Write("No file");
}
}

Causes

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

If I take out the "if" statement and just put

Response.Write(sFile);

Then it works fine. What is causing this error?

Thanks!
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top