C# 引发NullReferenceException?
我得到一个例外:C# 引发NullReferenceException?,c#,nullreferenceexception,C#,Nullreferenceexception,我得到一个例外: 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
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 { }
}
}
}
公共类类:基类
{
受保护名单;
公共对象对象对象1;
public ManualResetEvent MrEvent=新的ManualResetEvent(假);
只读属性1属性1;
只读属性2属性2;
只读事件处理程序BeginHandler、EndHandler;
只读事件处理程序句柄2;
只读事件处理程序句柄3;
公共类(Attr1 Attr1、Attr2 Attr2、EventHandler beginHandler、EventHandler endHandler、EventHandler handler2、EventHandler handler3)
{
this.Attr1=Attr1;
this.Attr2=Attr2;
this.BeginHandler=BeginHandler;
this.EndHandler=EndHandler;
this.Handler2=Handler2;
this.Handler3=Handler3;
螺纹t=新螺纹(运行);
t、 IsBackground=true;
t、 Start();
}
使方法无效()
{
Args1 Args1=新Args1();
if(BeginHandler!=null)
{
尝试
{
BeginHandler(本,args1);
}
捕获{}
}
尝试
{
List=GetList(args1);
}
捕获{}
如果(列表!=null)
{
List.Event1+=新的事件处理程序(List\u Event1);
}
if(Handler3!=null)
{
尝试
{
Args3 Args3=新的Args3(args1.Attr1,args1.Attr2);
Handler3(本,args3);
this.Object1=args3.Object1;
}
捕获{}
}
if(this.Object1==null)
{
如果(args1.Attr1!=null)
尝试
{
this.Object1=Class4.GetObject(args1.Attr1);
}
捕获{}
}
MrEvent.Set();
方法1();
方法2();
如果(列表!=null)
{
List.Event1-=新的事件处理程序(List\u Event1);
}
if(EndHandler!=null)
{
尝试
{
EndHandler(这个,args1);
}
捕获{}
}
}
}
我看不到任何地方可以抛出上面提到的句柄null异常。你看见谁了吗
To Veer:你怎么能把我问题的意思改变这么多?对于那些批准它的人:你怎么能同意它呢?在这段代码中,在try-catch块中,你不会得到null异常,因为catch块是空的,所以,异常不会被抛出,请检查其他对象 可以从内联方法引发NullReferenceException。查看Method1和Method2。空catch子句是有害的。
catch{}
关闭异常是有害的,非常有害的。我认为不可能从这段代码中找到异常。为什么不逐行调试并找到异常位置?只需调试您的项目,抛出异常的行就会突出显示。您真的在告诉我们,您可以编写多线程代码,但由于某些原因,您找不到F5键?它在你的键盘中部,在F6旁边,右边有一个F4。它启动调试器。我应该检查哪些其他对象?根据堆栈跟踪,此方法出现异常。是否从外部将参数发送到此构造函数(类)确保所有参数都不是nullCheck Method1();方法2();谢谢Vikram,我会检查它们,但是当异常从其中一个抛出时,它会在堆栈跟踪上,而不在那里。