0

Need some help here and I'm at a complete loss. I'm still relatively new to programming, and I'm doing a pretty big project for a company in helping them to automate their morning reports into Visual Studio on the 3.5 frame. So I'm getting the error:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:

Line 49:         clsMasterClass objMC = new clsMasterClass();
Line 50: 
Line 51:         if (!objMC.Read(new DateTime(2013, 06, 15)))
Line 52:             {
Line 53:             string error = objMC.LastError;
Line 54:             Response.Write(error.ToString());
Line 55:             // error handling
Line 56:             return;
Line 57:             } 

Now, this code, and the entire program works perfectly. It access the SQL database, makes a table and inserts the data. It then finds the spot in the arrays created that tell the data where to go, and places the data into that part of the image. Like I said, everything runs perfectly fine when you compile it, however the above error happens when you publish it, which is a bit beyond me, so my boss is handling that part.

Any help is greatly appreciated.

Here's the stack information when we got the error:

[NullReferenceException: Object reference not set to an instance of an object.]
   Proficy_Project.clsOptiSet.Read(DateTime ProdDate) in H:\Dev\Wallabee\Webpage\FinalProject\FinalProject\clsOptiSet.cs:70
   Proficy_Project.clsMasterClass.Read(DateTime ProdDate) in H:\Dev\Wallabee\Webpage\FinalProject\FinalProject\clsMasterClass.cs:36
   ASP.default_aspx.__Renderform1(HtmlTextWriter __w, Control parameterContainer) in c:\Inetpub\wwwroot\Wallaby\Default.aspx:51
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +163
   System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +32
   System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +51
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +40
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266
Ken White
  • 123,280
  • 14
  • 225
  • 444
Jordan1182
  • 11
  • 3
  • 1
    why are you doing an error.ToString() on a string object? there is no need to do a ToString() again. But that isn't the problem. Seems like you should follow the stack trace and step through the debugger line by line. My guess is there is an error happening in your clsMasterClass object in the method Read. Go look there...and post the entire stack trace of the exception. – Ahmed ilyas Nov 14 '13 at 19:22
  • Also, to elaborate in case you need more information. The date references an SQL function that pulls data from the companies database, and that is what inserts it into a table for ease of access. This program then pulls off that table and inserts it into the array. The classes in this program pull that 2013, 06, 15 and insert it into the SQL query as 2013-06-15 or 'yyyy-MM-dd' and this then runs the function which returns it back to the program. Sorry if this is back and forth, it's been a month or so and I'm only an intern here. – Jordan1182 Nov 14 '13 at 19:31
  • Updated the original post with the stack information. – Jordan1182 Nov 14 '13 at 19:36
  • So what does line 70 of clsOptiSet.cs look like? – 500 - Internal Server Error Nov 14 '13 at 19:55
  • `clsMasterClass` Hmm I wonder if this Is in fact a class? http://msdn.microsoft.com/en-us/library/vstudio/ms229040(v=vs.100).aspx – asawyer Nov 14 '13 at 19:55

1 Answers1

0

If you're new to programming, then I recommend something. To not get NullReferenceException, you should first check to see if nullable variables that you want to work with are null or not. They should not be null. For example in the following code:

public string GetFullName(Human human)
{
    return human.FirstName + ' ' + human.LastName;
}

you will get a NullReferenceException if human is null. Thus to get better exception messages, you should write:

public string GetFullName(Human human)
{
    if (human == null) 
        throw new ArgumentNullException("Human should not be null");
    return human.FirstName + ' ' + human.LastName;
}

You might be interested in reading these articles:

Why doesn't “object reference not set to an instance of an object” tell us which object?
and
Object reference not set to an instance of an object.Why doesn't .NET show which object is `null`?

Community
  • 1
  • 1
Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
  • Gotcha, I'll give that a try immediately. But yeah, before I started this project, I only had some experience in C, but none in C# and we did this in about 3 months. So I'm quite new to it, especially since half that time was SQL – Jordan1182 Nov 14 '13 at 19:55
  • And it seems that you should show us line 70 of your `clsOptiSet` file. – Saeed Neamati Nov 14 '13 at 19:56
  • It won't let me add line 70. But it's just: Line 70: adp.Dispose(); Which is just me closing my try catch finally. – Jordan1182 Nov 14 '13 at 20:12
  • Awesome, we got it figured out guys. The adp.dispose needed to be if(adp != null) adp.dispose, along with the cnn as well. Once that happened, it wasn't getting caught up on a null dispose and worked perfectly. Now I just have to go back in and fix the sql connection to include the id/password and we're off. Thanks a lot guys. Anything I need to do to close this up? – Jordan1182 Nov 14 '13 at 20:22