Problem checking the lenght of a string

  • Thread starter Thread starter vbMark
  • Start date Start date
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!
 
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
 
I would guess that the value that you are searching for in the QueryString is null. Check your spelling/case.
 
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!
 
Back
Top