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
4

1 回答 1

0

如果您是编程新手,那么我推荐一些东西。要不获取NullReferenceException,您应该首先检查您要使用的可为空变量是否为空。它们不应为空。例如在以下代码中:

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

你会得到一个NullReferenceExceptionifhuman为空。因此,为了获得更好的异常消息,您应该编写:

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

您可能有兴趣阅读这些文章:

为什么“对象引用未设置为对象的实例”不告诉我们哪个对象?
并且
对象引用未设置为对象的实例。为什么.NET 不显示哪个对象是“null”?

于 2013-11-14T19:50:22.113 回答