如何修复空引用异常? [英] How to fix a null reference exception?

查看:116
本文介绍了如何修复空引用异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个连接数据库的网络表单。当用户输入并点击提交表单时,我想存储他们在每个字段中输入的值。我已经调试了下面的代码,似乎无法看到对象引用未被设置为对象实例的位置。我甚至检查过使用相应的用户条目更新了值。



I currently have a web form that is connected a database. When a user makes an entry and hit the submit form I want to store the values they have entered in each field. I've debugged the code below and can't seem to see where an object reference is not being set to an instance of an object. I've even checked to see the value value is updated with the corresponding user entry.

protected void uxSubmitBtn_Click(object sender, EventArgs e)
{
    var accountTrackerEntrySVC = new AccountTrackerEntrySVC();

    var result = ViewState["AccountEntryID"] == null ?
    accountTrackerEntrySVC.InsertAccountTrackerEntry(//uxAccountNumTxt.Text.Trim(),
    DataConverter.StringToInteger(ViewState["DataCustodianWorkerNum"].ToString()),
    uxDataBeingTransferredDescTxt.Text.Trim(),
    uxPurposeOrFunctionDialInDescTxt.Text.Trim(),
    uxPhysicalLocationTxt.Text.Trim(),
    uxPurposeOrFunctionDialOutDescTxt.Text.Trim(),
    uxApplicationUtilizingModemDescTxt.Text.Trim(),
    uxPasswordChangeFrequencyDescTxt.Text.Trim(),
    uxOtherLayerAuthenticationDescTxt.Text.Trim(),
    uxMachineServerModeConnectionDescTxt.Text.Trim(),
    CommonSVC.GetUserInfoFormattedFromSession(),
    uxTypeOfconnectionDescTxt.Text.Trim()
    ,modemRequirementCB.Checked
    ,ModemConnectionToNetworkDeviceCB.Checked
    ,PasswordProtectedCB.Checked) :
    accountTrackerEntrySVC.UpdateAccountTrackerEntry(DataConverter.StringToInteger(ViewState["AccountEntryID"].ToString()),
    CommonSVC.GetUserInfoFormattedFromSession(),
    uxDataBeingTransferredDescTxt.Text.Trim(),
    uxPurposeOrFunctionDialInDescTxt.Text.Trim(),
    uxPhysicalLocationTxt.Text.Trim(),
    uxPurposeOrFunctionDialOutDescTxt.Text.Trim(),
    uxApplicationUtilizingModemDescTxt.Text.Trim(),
    uxPasswordChangeFrequencyDescTxt.Text.Trim(),
    uxOtherLayerAuthenticationDescTxt.Text.Trim(),
    uxMachineServerModeConnectionDescTxt.Text.Trim(),
    uxTypeOfconnectionDescTxt.Text.Trim(),
    modemRequirementCB.Checked,
    ModemConnectionToNetworkDeviceCB.Checked,
    PasswordProtectedCB.Checked);

    if (result.Successful)
    {
        uxInfoMsg.DisplayMessage(result.Message, InfoMessage.InfoMessageType.Success);
    }
    else
    {

    }
    //Hide progress indicator
    Master.HideProgressIndicator();
}





也许另一双眼睛会看到我还没有发现的错误。对于c#来说还是比较新的所以我会尽力发布你可能遇到的任何问题的回复。谢谢!



Perhaps another pair of eyes will see the error I have yet to spot. Still fairly new to c# so I'll do my best to post responses to any questions you may have. Thanks!

推荐答案

我不确定,因为我在过去调试时会突出显示代码失败的行。在这种情况下,我发布的整个代码块正在突出显示。



这意味着它是您传递给<的一个(或多个)参数code> InsertAccountTrackerEntry 方法。

可能你的一个ViewState名称是错误的 - 例如,Custodian在其他地方拼写为Custodain或类似名称。

突破你的变量:

"I'm unsure because when I've debugged in the past it will highlight the line where the code fails. In this case the entire block of code I've posted is being highlighted."

Which means it's one (or more) of the parameters you are passing to the InsertAccountTrackerEntry method.
Probably, one of your ViewState names is wrong - "Custodian" spelt elsewhere as "Custodain" or similar for example.
"break out" your variables:
object dcwn = ViewState["DataCustodianWorkerNum"];

在调用之前,使用调试器找出哪一个在你调用ToString并将它们传递给你的方法之前是null。



当你知道哪个是null时,你可以看看找出原因。

before the call, and use the debugger to find out which one is null before you call ToString on them and pass them to your method.

When you know which is null, you can look at finding out why.


实际上不可能根据你的代码说哪个变量是null。



你能做的最好的事情就是在 var结果上放置一个断点= ViewState ... 代码块,当执行停止时,使用鼠标遍历每个对象并查看什么是null。或者,您可以使用Watch窗口或快速监视
It's actually impossible to say which variable is null based on your code.

The best thing you can do is that you place a breakpoint on the var result = ViewState... code block and when the execution stops over there, using mouse go over each object and see what is null. Alternatively you can use the Watch window or quick watch.


这篇关于如何修复空引用异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆