为什么我有时会在Winforms应用程序中获得System.NullReferenceException?[副本]

hgc7kmma  于 2023-06-06  发布在  其他
关注(0)|答案(1)|浏览(167)

此问题已在此处有答案

What is a NullReferenceException, and how do I fix it?(26答案)
3天前关闭。
它很少发生,只在调试模式下发生过,但它让我发疯。这是它发生的函数:(在表单后面的代码中)

public override bool Func()
        {
            if (this.InvokeRequired)
            {
                return (bool)this.Invoke((Func<bool>)delegate
                {
                    return this.SomeRadioButton.Checked;  // Happens in this line
                });
            }
            else
            {
                return this.SomeRadioButton.Checked;
            }
        }

这是个例外(出于隐私原因,我只是编辑了名称和路径)

System.NullReferenceException: Object reference not set to an instance of an object.
   at Project.Forms.Layouts.SomeForm.Func() in C:\Users\.........cs:line 2824
   at Project.Forms.MainForm.backgroundWorkerDoWorkFunc() in C:\Users\.......cs:line 5091  
   at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
   at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
   at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(IMessage msg, IMessageSink replySink)
   at System.Runtime.Remoting.Proxies.AgileAsyncWorkerItem.DoAsyncCall()
   at System.Runtime.Remoting.Proxies.AgileAsyncWorkerItem.ThreadPoolCallBack(Object o)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

我不知道为什么这个bug会发生,它很少发生。
非常感谢。
我不知道你为什么关闭我的问题,这是不一样的What is a NullReferenceException, and how do I fix it?,因为没有答案的情况下有

8fsztsew

8fsztsew1#

确保bg worker在组件初始化之后启动。我想她对你的拖放工人的命令是在初始化之前的。这样,第一个工作在创建某个单选按钮之前发生。

相关问题