0

This code is invoked by delegate:

private void OnMsgBoxClick(object arg)
{
    if (arg.GetType() == typeof(int))
    {
        this.LogWarning("OnMsgBoxClick() arg: {0}", arg.ToString());
    }
}

and it gives NullReferenceException, but still debug this sentence after like this:

NullReferenceException: Object reference not set to an instance of an object
> 21:29:11.777 Example_MsgBox::Invoke() OnMsgBoxClick() arg: 0

and I want to get the debuger without error. How can I deal with this question?

Debuger Screenshot


This is my Error Call Stack.

NullReferenceException: Object reference not set to an instance of an object
Example_MsgBox.OnMsgBoxClick (System.Object arg) (at Assets/GamePlay/Example/Example_MsgBox.cs:28)
GamePlay.UI.UIAPI+<ShowMsgBox>c__AnonStorey0.<>m__0 (System.Object closeArg) (at Assets/GamePlay/UI/UIAPI.cs:32)
SGF.UI.FrameWork.UIWindow.OnDisable () (at Assets/SGF/UI/FrameWork/UIWindow.cs:73)
UnityEngine.GameObject:SetActive(Boolean)
SGF.UI.FrameWork.UIWindow:Close(Object) (at Assets/SGF/UI/FrameWork/UIWindow.cs:119)
GamePlay.UI.Common.UIMsgBox:OnBtnClick(Int32) (at Assets/GamePlay/UI/Common/UIMsgBox.cs:62)
UnityEngine.EventSystems.EventSystem:Update()

This is 'UIMsgBox':

    public void OnBtnClick(int btnIndex)
    {
        if (btnIndex.GetType() == typeof(int))
        {
            this.Close(btnIndex);
        }
    }

This is 'UIWindow':

    public delegate void CloseEvent(object arg = null);
    public event CloseEvent OnCloseEvent;
    public sealed override void Close(object arg = null)
    {
        this.Log("Close() arg:{0}", arg);
        if (this.gameObject.activeSelf)
        {
            this.gameObject.SetActive(false);
        }
        OnClose(arg);
        if (OnCloseEvent != null)
        {
            OnCloseEvent(arg);
            OnCloseEvent = null;
        }
    }

Sorry, I think it's too long.

Thanks!!!

  • Is there a possibility you invoke `OnMsgBoxClick` twice? – Charlie Malmqvist Jun 29 '18 at 14:05
  • No, I'm sure that it just invoke once. And I think it maybe the delegate running first, and then get the arg. But I'm not sure of my guess. – Chan.Wunsam Jun 29 '18 at 14:08
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – gunr2171 Jun 29 '18 at 14:14
  • @gunr2171 Did you read the post? – Charlie Malmqvist Jun 29 '18 at 14:15
  • Yes I did read the post. – gunr2171 Jun 29 '18 at 14:15
  • @gunr2171 Thanks, I had read that question. But I still don't know how to deal with my question. – Chan.Wunsam Jun 29 '18 at 14:31
  • Show us how you are invoking this delegate. – AresCaelum Jun 29 '18 at 14:47
  • I jumped the gun on the comment I removed, however I am curious as to why in your screenshot you have no normal log messages? When you are clearly logging something? The only thing I can think of is either you are not showing us the right code, or you added that log statement to the code your showing us... `this.Log("Close() arg:{0}", arg);` isn't in your screenshot – AresCaelum Jun 29 '18 at 15:22
  • Also, You may want to try deactivating your gameObject at the end of the function. – AresCaelum Jun 29 '18 at 15:23
  • It looks like your error is caused by a messagebox that's triggered by `this.gameObject.SetActive(false)` while the log line you want is caused by `OnCloseEvent(arg);`. @mihir_Dave's answer should be correct but it might be that `SetActive` sets your `OnCloseEvent` to null and that the error is preventing that – Jan-Peter Vos Jun 29 '18 at 15:27
  • Thanks. I don't know why normal log will disappear, so maybe it is anthor bug. And I try to debug these tonight. – Chan.Wunsam Jun 29 '18 at 15:36

1 Answers1

3

Add this check.

private void OnMsgBoxClick(object arg)
{
    if (arg != null && arg.GetType() == typeof(int))
    {
        this.LogWarning("OnMsgBoxClick() arg: {0}", arg.ToString());
    }
}
Mihir Dave
  • 3,954
  • 1
  • 12
  • 28