Message Listener: Object Reference not set to an instance of an Object

Announcements, support questions, and discussion for Quest Machine.
Post Reply
shortestyard57
Posts: 29
Joined: Fri Jun 02, 2023 8:31 am

Message Listener: Object Reference not set to an instance of an Object

Post by shortestyard57 »

Hey Tony,

I have been unsuccesful in troubleshooting the following error.

Message System exception sending 'ImperiumAdvanceCamp'/'Start' to Machines and Managers (GlobalVariablesAndMessageTracker): Object reference not set to an instance of an object

Here is the console message:

Code: Select all

[Error] Message System exception sending 'ImperiumAdvanceCamp'/'Start' to Machines and Managers (GlobalVariablesAndMessageTracker): Object reference not set to an instance of an object
MessageSystem.SendMessageWithTarget() at /3rdParty/Tools/Pixel Crushers/Common/Scripts/Message System/MessageSystem.cs:370
 368:      catch (System.Exception e)
 369:      {
--> 370:          Debug.LogError("Message System exception sending '" + message + "'/'" + parameter + "' to " + x.listener + ": " + e.Message);
 371:      }
 372:  }

MessageSystem.SendMessage() at /3rdParty/Tools/Pixel Crushers/Common/Scripts/Message System/MessageSystem.cs:430
 428:  public static void SendMessage(object sender, string message, string parameter, params object[] values)
 429:  {
--> 430:      SendMessageWithTarget(sender, null, message, parameter, values);
 431:  }

ImperiumAdvancedCampSceneStart/<SceneLoad>d__3.MoveNext() at /_Project/Scenes/Imperium Advanced Camp/ImperiumAdvancedCampSceneStart.cs:36
  34:      //Message to set quest active (Journal and HUD are tied to quest being active)
-->  36:      MessageSystem.SendMessage(this.gameObject, "ImperiumAdvanceCamp", "Start");
  38:  }

SetupCoroutine.InvokeMoveNext()
And here is where I add the listener:

Code: Select all

private void OnEnable()
    {
        messageList = new();
        questIDs = new();
        foreach (var quest in questDatabase.questAssets)
        {
            questIDs.Add(quest.id.ToString());
        }
        foreach (var questID in questIDs)
        {
            if (questID != null)
            {
                MessageSystem.AddListener(this, questID, string.Empty);
            }            
        }

    }
I thought the null object was the sender but after playing around with sending difference references and I get the name of the sender object to populate in the console via debug.log (I have tried class instance, gameobject, and object) I am not sure. Is there something else that could be the object in question?
User avatar
Tony Li
Posts: 22102
Joined: Thu Jul 18, 2013 1:27 pm

Re: Message Listener: Object Reference not set to an instance of an Object

Post by Tony Li »

Hi,

The error is in your OnMessage() method. In your code editor, add a breakpoint in your method and step through to identify which line is the culprit.

Quest Machine's Message System catches exceptions and reports them. This allows Quest Machine to continue functioning even if an OnMessage() method throws an exception. The downside is that it's a bit harder to identify which line in the OnMessage() method caused the exception.
shortestyard57
Posts: 29
Joined: Fri Jun 02, 2023 8:31 am

Re: Message Listener: Object Reference not set to an instance of an Object

Post by shortestyard57 »

Makes sense and indeed that was the issue. Thanks as always.
User avatar
Tony Li
Posts: 22102
Joined: Thu Jul 18, 2013 1:27 pm

Re: Message Listener: Object Reference not set to an instance of an Object

Post by Tony Li »

Glad to help!
Post Reply