【问题标题】:Weird Error: Object reference not set to an instance of an object奇怪的错误:对象引用未设置为对象的实例
【发布时间】:2013-02-10 11:00:36
【问题描述】:

我有时会在实时应用程序中遇到错误。

堆栈跟踪:

在 Tool.User_RequestSender.Page_Load(Object sender, EventArgs e) 中 d:\Site\Tool\User\RequestSender.aspx.cs:第 72 行 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,对象 o,对象 t,EventArgs e) 在 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(对象发送者, EventArgs e) 在 System.Web.UI.Control.OnLoad(EventArgs e) 在 System.Web.UI.Control.LoadRecursive() 在 System.Web.UI.Page.ProcessRequestMain(布尔值 includeStagesBeforeAsyncPoint,布尔型 includeStagesAfterAsyncPoint)

我一次又一次地尝试在我的机器上运行代码,但没有得到同样的错误。

即使我是实时代码,我也会偶尔遇到这个问题。

RequestSender.aspx.cs 中的Page_Load 代码:

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                this.LoadRequest();
            }
        }
        catch (CustomException ex)
        {
            this.ShowMessage(ex.Message);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

LoadRequest()如下:

private void LoadRequest()
    {
        Credential credential;


            if (
                !string.IsNullOrEmpty(Request["typeOfrequest"]) &&
                !string.IsNullOrEmpty(Request["empUserId"]) &&
                !string.IsNullOrEmpty(Request["applicationId"])
               )
            {
                // Get details of credentials
                credential = (new RequestManager()).GetCredential(this.EmpUserId, this.ApplicationId, (int)this.TypeOfrequest);
                this.applicaionRequest = new Request
                {
                    RequestType = this.TypeOfrequest,
                    RequestStatus = Enumerations.RequestStatus.Sent,
                    Application = credential.Application,

                    EmpUserId = credential.EmpUserId,
                    EmployeeId = credential.EmployeeId,
                    Username = credential.Username,

                    SenderAddress = Security.CurrentUser.Details.EmailAddress,
                    ReceiverAddress = (new Datastore.RequestStore()).GetReceiverAddress((int)this.TypeOfrequest, this.ApplicationId),
                    AddedBy = Security.CurrentUser.Details.UserId
                };
                    ucSendRequest.ApplicationRequest = applicaionRequest;

            }
            else
            {
                Response.Write("Invalid request!");
                Response.End();
            }
        }

    }

【问题讨论】:

  • 您拥有catch(Exception ex) { throw ex; } 的事实掩盖了最初的原因。摆脱那个 catch 块,你会更好地了解发生了什么。
  • 什么是 ucSendRequest?
  • Exaclty...但我无法在我的机器上出错...即使我删除了 try catch 我也不会在我的机器上再次收到任何错误或问题...
  • @mihirj:是用户控制
  • 捕获异常然后发出“throw ex”会替换堆栈跟踪,这意味着您找到异常实际来自何处的机会较小。如果您需要重新抛出您已捕获的异常,请在 catch 块内单独使用“throw”。如果你的 catch 块中除了 throw 之外什么都没有,那么你一开始就不需要 catch 块。

标签: c# asp.net


【解决方案1】:

错误清楚地表明 smth 在应用程序执行期间是null

要获得任何帮助,您应该检查 null 值(LoadRequest() 中的 Request 可能是一个不错的选择),或者,更好的解决方案是修改您的 catch 博客以记录导致应用程序崩溃的参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 2012-08-31
    • 2011-11-21
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多