【问题标题】:NullReferenceException thrown?抛出 NullReferenceException?
【发布时间】:2013-05-30 14:43:07
【问题描述】:

我得到了这个例外:

NullReferenceException
Object reference not set to an instance of an object
   at Namespace.TheClass.TheMethod()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

在这段代码中:

public class TheClass : TheBaseClass
{
    protected IList1 List;
    public object Object1;
    public ManualResetEvent MrEvent = new ManualResetEvent(false);
    readonly Attr1 Attr1;
    readonly Attr2 Attr2;
    readonly EventHandler<Class1> BeginHandler, EndHandler;
    readonly EventHandler<Class2> Handler2;
    readonly EventHandler<Class3> Handler3;

    public TheClass(Attr1 attr1, Attr2 attr2, EventHandler<Class1> beginHandler, EventHandler<Class1> endHandler, EventHandler<Class2> handler2, EventHandler<Class3> handler3)
    {
        this.Attr1 = attr1;
        this.Attr2 = attr2;
        this.BeginHandler = beginHandler;
        this.EndHandler = endHandler;
        this.Handler2 = handler2;
        this.Handler3 = handler3;
        Thread t = new Thread(Run);
        t.IsBackground = true;
        t.Start();
    }

    void TheMethod()
    {
        Args1 args1 = new Args1();
        if (BeginHandler != null)
        {
            try
            {
                BeginHandler(this, args1);
            }
            catch { }
        }

        try
        {
            List = GetList(args1);
        }
        catch { }

        if (List != null)
        {
            List.Event1 += new EventHandler<Args2>(List_Event1);
        }

        if (Handler3 != null)
        {
            try
            {
                Args3 args3 = new Args3(args1.Attr1, args1.Attr2);
                Handler3(this, args3);
                this.Object1 = args3.Object1;
            }
            catch { }
        }

        if (this.Object1 == null)
        {
            if (args1.Attr1 != null)
                try
                {
                    this.Object1 = Class4.GetObject(args1.Attr1);
                }
                catch { }
        }

        MrEvent.Set();
        Method1();
        Method2();

        if (List != null)
        {
            List.Event1 -= new EventHandler<Args2>(List_Event1);
        }

        if (EndHandler != null)
        {
            try
            {
                EndHandler(this, args1);
            }
            catch { }
        }
    }
}

我没有看到任何地方可以抛出上述句柄空异常。你看到有人了吗?

致 Veer:你怎么能如此改变我的问题的意思?
致那些批准它的人:你怎么能同意它?

【问题讨论】:

  • 空的 catch 子句是邪恶的。
  • catch { } 关闭异常很糟糕,非常糟糕。
  • 我认为不可能从这段代码中找到异常。为什么不逐行调试,找到异常的地方呢?
  • 只要调试你的项目,抛出异常的那一行就会被高亮显示。
  • 您真的告诉我们,您可以编写多线程代码,但由于某种原因您找不到 F5 键吗?它位于上排键盘的中间,紧邻 F6 和 F4 的右侧。它启动调试器。

标签: c# nullreferenceexception


【解决方案1】:

NullReferenceException 可以从内联方法中抛出。查看方法1和方法2。

【讨论】:

    【解决方案2】:

    在这段代码中,在try catch块内你不会得到空异常,因为catch块是空的,所以异常不会抛出一边,检查其他对象。

    【讨论】:

    • 我应该检查哪些其他对象?根据堆栈跟踪,此方法异常。
    • 你是从外部向这个构造函数(TheClass)发送参数
    • 确保所有参数不为空
    • 检查方法1();方法2();
    • 谢谢 Vikram,我会检查它们,但是当其中一个抛出异常时,它会在堆栈跟踪中并且不存在。
    猜你喜欢
    • 2020-11-17
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多