Object reference not set to an instance of an object.?? Please Help

F

Frawls

Hi,

I get the following error when trying to run a search on my aspx site,
this error only occours if the product im searching for does not
exist. Can anybody explain this please and help me with a solution, i
know its probably simple but im new to the game...Cheers

-----------------------------------------------------------------------

Object reference not set to an instance of an object.

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

Source Error:


Line 63: {
Line 64: strSearchValue = Request.QueryString["sv"];
Line 65: lblSearch.Text = "Your search for <b>'" +
strSearchValue + "'</b> brought back <b>no results</b>" ;
Line 66: }
Line 67:




Stack Trace:


[NullReferenceException: Object reference not set to an instance of an
object.]
woodies.searchResults.Page_Load(Object sender, EventArgs e) in
c:\inetpub\wwwroot\woodies\searchresults.aspx.cs:65
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750


-----------------------------------------------------------------


And here is the some of the code from the page which is prob relevant:


------------------------------------------------------------------

using System.Web.UI.WebControls;

..
..
..
public class searchResults : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblSearch;

..
..
..

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (Request.ServerVariables["HTTPS"].ToString() !="off")
{siteImages = ConfigurationSettings.AppSettings["SiteImagesSecure"];}

if (Request.QueryString["sv"] != null && Request.QueryString
["sv"].ToString() != "")
{
strSearchValue = Request.QueryString["sv"];
lblSearch.Text = "Your search for <b>'" + strSearchValue + "'</b>
brought back <b>no results</b>" ;
}
 
P

Peter Rilling

I hate to be picky, but have you actually stepped through your code in an
IDE and evaluated the variables being used? Unlike some other exceptions,
this one is pretty consistent and predictable. The error message itself is
not cryptic.

Step through your code and see what the value is for your variables at the
point where it fails. One of your variables has no value yet you are trying
to use it.
 
T

Tyler

Could it be that your lblSearch (I'm assuming a label control) has not yet
been instantiated? I'm not too familiar with ASP.NET, but is it possible
that the control is created after the Page_Load method is invoked and so you
need to wait until a later point in time to reference the control?

Tyler
 
F

frawls

Thanks for the advice guys, much appreciated. The problem was that i
did not have an ASP:label with an ID= lblSearch in my search
Results.aspx page. I no longer recieve the error message, however the
page now seems to try to load and reload infinately without displaying
any information.... So more work for me....

Thanks again,
frawls
 

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