NullReferenceException from IsInvoking, CancelInvoke

Hello, I’m a developer who runs mobile games targeting iPhone, android.
I got NullReferenceException error from IsInvoking, CancelInvoke method.
This problem is occurring only for some users, so I couldn’t reproduced this error.
This is a problem that occurs on both Android and iOS platforms.
The error continues to occur only with some users, and the same symptoms are repeated when the user deletes and reinstalls the app.
Originally, only the CancelInvoke method was used, but a nullReferenceException error occurred, so I added IsInvoking, but this time, IsInvoking have that error.

Sentry error log:
9411272--1317980--upload_2023-10-16_14-37-24.png
Error Occured Code 1:

public override void KillSkill()
    {
        try
        {
            if (IsInvoking("Timer"))    // NullReferenceException ERROR! (line 114)
                CancelInvoke("Timer");
            isActivate = false;
            supportImage?.gameObject.SetActive(false);
            if (effectObject != null)
                if (effectObject.TryGetComponent(out ParticleSystemRenderer particleSystemRenderer))
                {
                    particleSystemRenderer.material.DOFade(0, 1f).OnComplete(() =>
                    {
                        if (effectObject.TryGetComponent(out ParticleSystem particleSystem))
                        {
                            particleSystem.Stop();
                        }
                        particleSystemRenderer.material.color = Color.white;
                        effectObject.SetActive(false);
                    });
                }
        }
        catch (Exception e)
        {
            GlobalLogCatch.instance.LogCatch(e.Message, e.StackTrace, LogType.Error);
            Sentry.SentrySdk.CaptureException(e);
        }
        finally
        {
        }

Sentry error log:
9411272--1317983--upload_2023-10-16_14-38-22.png
Error Occured Code 2:

void SkillDuration()
    {
        if (IsInvoking("Timer")) CancelInvoke("Timer");    // NullReferenceException ERROR! (line 99)
        timer = duration;
        InvokeRepeating("Timer", 0, 0.1f);
    }

Timer method for invoke :

    void Timer()
    {
        if (timer > 0)
        {
            timer -= 0.1f;
        }
        else
        {
            if (IsInvoking("Timer"))
                CancelInvoke("Timer");
            KillSkill();
        }
    }

As you can see from the code, we only saw that the method had written on the server that a Null ReferenceException occurred, and we checked all possible problems within the method.
However, due to the continuous problem, we eventually used Sentry (which allows us to track the line number of the IL2CPP-built apps) to track exactly which line the error occurred, and as a result, we found that the Invoke-related method had an error.

When is KillSkill being called? If it were to happen in say, OnDestroy, I imagine there might be some race condition going on with Unity fake nulls perhaps?

Mind you I would outright avoid the use of Invoke, InvokeRepeating, etc, for stuff like this (hell I wouldn’t use them at all these days, they’re very old API methods). More reliable to just have a timer ticking in Update that invokes what you need at set intervals, with a means to turn it off and on. That way you have full control over its behaviour, and aren’t beholden to the mystery black-box of Unity’s internals.

And you already have a timer going on anyway. Why not just use that?

Thank you for your quick reply.
Of course, when I first wrote the code, I implemented the timer function through the Update method.
However, the timer did not work normally for some users of low-performance devices, or users who try to drop frames for abuse purposes.
During the live service, I used Invoke as a result of writing the code while checking the actions (attack speed, movement speed, etc.) that I had to match with the timer in a hurry to fix the problem.
And that code is not destroyed, it’s still alive.

These newfangled C# ? and ?? operator DO NOT WORK with any object derived from anything in the Unity engine.

Otherwise…

The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

Thank you for your answer.
But the NullReferenceException error didn’t occur that point. It occured at IsInvoking("Timer") and CancelInvoke("Timer").

Null references don’t “occur at.”

They are the result of attempting to dereference a null value.

Go back and start with Step #1 and don’t stop until you know THIS IDENTIFIER HERE IS NULL

Remember that any method invoked within a MonoBehaviour is always:

this.MyMethod();

And that this can become null by calling Destroy()

Okay. NullReferenceException error is not occured, it is the result.

I don’t know the difference, but I see.

this.MyMethod();

this cannot become null because it is attached to the object, dontdestroyonload.

And also, there is no code anywhere which call Destroy() this script.

When you see error log from sentry at my first post,

ChosunBuff.cs is this script.

NullRefrenceException error result is from SkillDuration() method, and that method is called from SkillAction() method.

That mean ChosunBuff.cs is not null.

The exception tells you which line you’re deferencing a NULL. You need to debug that and find out why it’s NULL. Unfotunately, posting on the forum won’t give you the answer, especially if you cannot reproduce it because nobody here could either.

You either reproduce it or you try to figure out under what circumstance the line in question could possibly become NULL.

I know it’s not always easy to do that with a lot of moving parts.

That was a lot of writing but it sounds like you still have not completed Step #1: identify what is null.

It will be exceedingly difficult to fix your problem until you find your problem, but perhaps you will get lucky.

Meanwhile, when you are ready to fix the problem… I would start at line 99 and reason about what’s going on there.

The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

Finally, I fixed this problem.

It was only after fixing that I understood what Kurt-dekker was talking about.

So far, the error has only occurred when a reference didn’t set from the inspector window or when a reference has disappeared, so I have been focusing only on the code of the corresponding line in the log.

However, after a slow examination from the root, I found that there was a problem with the reference of the ChosunBuff.cs file among the codes written by ScriptableObject.

During the initialization process during runtime, I assigned ChosunBuff.cs , one of the elements needed for the code that was written as ScriptableObject.

However, it is presumed that the assigned value is missing without knowing what reason, while the game was in progress.

It’s been a few days since I corrected the code for this part just in case, but there’s no more error.

Thank you!