Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NullReferenceException

Status
Not open for further replies.

organicg

Programmer
Oct 21, 2002
151
US
I know this would be thrown if trying to access an object that's null, but it's only happening sporadically which makes it hard to test. This error is emailed to me, and it doesn't have the line number in it, not sure why. That would help.

System.NullReferenceException: Object reference not set to an instance of an object.
at SC.WebModules.Cars.Web.SearchResults.Page_Load(Object sender, EventArgs e)...

Code:
		private void Page_Load(object sender, System.EventArgs e)
		{
			Message.InnerHtml = "";
			if (Session["Message"] != null)
			{
				Message.InnerHtml = Session["Message"].ToString();
				Session.Remove("Message");
			}

			if (Session["dvProfiles"] != null && ((DataView)Session["dvProfiles"]).Count > 0)
			{
				if (Request.UrlReferrer.AbsolutePath.IndexOf("SendEmail.aspx") != -1 && Session["SavedPageIndex"] != null)
				{
					ProfilesGrid.CurrentPageIndex = (int)Session["SavedPageIndex"];
				}
				ProfilesGrid.DataSource = (DataView)Session["dvProfiles"];
				ProfilesGrid.DataBind();
			}
			else
			{
				Message.InnerHtml = "No profiles matched your search criteria.";
				searchAgain.Visible = false;
			}

		}
 
This doesn't address the exact error directly, but you may want to add a try/catch block and having the Exception ToString() sent to you in the email to get the line number.
 
I get the error from Server.GetLastError() and email the myError.ToString(), but it doesn't have the line number. Any idea why?
 
Here's a snip of something that I use in my error page (page_load). It sends me an email with the error, stack trace, user, line number and all the controls (if any) on the form along with their values at the time of the error.
Code:
<snip>
Exception ex = Server.GetLastError();
			if(ex != null)
			{
				try
				{
					StringBuilder sb = new StringBuilder();
					if(ex.InnerException != null)
					{
						sb.Append("Error: " + ex.InnerException.Message);
						sb.Append(Environment.NewLine);
						sb.Append("Source: " + ex.InnerException.Source.ToString());
						sb.Append(Environment.NewLine);
						sb.Append("User: " + BizHelp.LanID().ToString());
						sb.Append(Environment.NewLine);
						sb.Append(Environment.NewLine);
						sb.Append("Stack Trace: " + Environment.NewLine);
						sb.Append(ex.InnerException.StackTrace.ToString());
									
					}
					string[] strKeys;
					string[] strValues;
					string strControls = "";
					strKeys = Request.Form.AllKeys;

					for(int i = 0;i<Request.Form.Count;i++)
					{
						strValues = Request.Form.GetValues(i);
						for (int j = 0; j <= strValues.GetUpperBound(0); j++) 
						{
							if (strKeys[i] != "__VIEWSTATE")
								strControls += strKeys[i] + "=" +
									strValues[j] + "\r";
						}

					}
</snip>
// send email and write to the database errorLog table here...
// also add your catch
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top