-1

It happens rarely and has happened to me only in debugging mode, but it drives me crazy. this is the function it happens in: (in the code behind of form)

        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;
            }
        }

and this is the exception: (I just edited the names and paths for privacy reasons)

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()

I don't know why this bug happens, it happens rarely.

Thanks very much.

I don't know why you closed my questions, it's not the same as What is a NullReferenceException, and how do I fix it? because there is no answer for this scenario there

yo245
  • 1
  • 1
  • 1
    Don't do this. Calling invoke this way is blocking. I'd like to see a [mcve] of your entire design before suggesting an alternative, but this way is not the right way. – Enigmativity Jun 03 '23 at 04:47
  • 1
    Errors that happen in debug mode are generally much easier to trace. Just look at the state of everything in the debugger; particularly the other threads that are running at the same time. I suspect the background worker is created as part of the form constructor, and happens to run _before_ the rest of the constructor finishes. _Never_ run virtual methods in a constructor, and be very careful about anything asynchronous you run from a constructor (don't leak `this` from a constructor!). – Luaan Jun 03 '23 at 04:48
  • @Luaan no, this function is running after the constructor finishes for sure, and its happen rarely so its hard to trace it :( – yo245 Jun 03 '23 at 06:25
  • 1
    The question has been closed because you did not provide enough context to understand when and in what circumstances the exception is generated. I.e.,, the problem cannot be reproduced with the code provided -- If you're invoking from the `DoWork` event handler of a BackGroudWorker, then don't. Never, ever do that. The BGW provides events to update the UI, these events are raised in the UI Thread. Furthermore, `InvokeRequired()` doesn't protect at all from exceptions that may be caused by disposed elements (and you cannot test for a disposed element from a Thread other than the UI Thread) – Jimi Jun 03 '23 at 14:20

1 Answers1

0

Make sure that bg worker starts AFTER components initialization. I susoct that she to order of your drag an drop bg worker is before init tgan rest of form. This way first work happen before some radio button is created.