简体   繁体   English

对象引用不是设置为对象的实例,而是吗?

[英]Object reference not set to an instance of an object, but is?

I must be missing something here, but I've got an instance of my submission object called up, in scope, and when I try to use it in another line, I throw this error about not setting the reference to an instance. 我必须在这里丢失一些东西,但是我有一个提交对象的实例在范围内被调用,当我尝试在另一行中使用它时,我抛出了关于不设置对实例的引用的错误。 What gives? 是什么赋予了?

protected void Page_Load(object sender, EventArgs e)
{

    string x = Request.QueryString["SubmissionId"];

    SubmissionService ss = new SubmissionService();
    Submission sub = ss.getSubmissionByID(x);

if (sub.Status1.Equals(0))
    {
        PanelComment.Visible = false;

    }
    else

These could be null: 这些可以为空:

ss 
sub
sub.Status1
x

Put a breakpoint, and debug... 放置一个断点,然后调试...

您的提交对象“ sub”上的Status1属性可能为null ...

Given the code that you supplied, there can be three causes: 给定您提供的代码,可能有三个原因:

  1. the "Status1" property is null, this would generate a NullReferenceException. “ Status1”属性为null,则将生成NullReferenceException。
  2. PanelComment is null PanelComment为空
  3. sub is null 子为空

Given the code, the following properties could be null: 给定代码,以下属性可以为null:

  • Submission sub (If there is no submission with the ID = x) Submission sub(如果没有ID = x的提交)
  • sub.Status1 sub.Status1
  • List item 项目清单

PanelComment PanelComment

Just change your code by the following: 只需通过以下方式更改代码:

Submission sub = String.IsNullOrEmpty(x) ? null : ss.getSubmissionByID(x);

if (sub!= null && sub.Status1 == 0){
...

It should fix most of the null references. 它应该修复大多数空引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM