playMaker

Author Topic: How to track down NullReferenceException  (Read 1805 times)

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 256
How to track down NullReferenceException
« on: March 01, 2023, 10:18:45 AM »
It's one of the very few things I really don't like with Playmaker and not sure if there is a way to fix it with an update.

But when you get a NullReferenceException error the entire project stops to work and random FSMs stop to execute because of it. The worst part though: it's almost impossible to track down unless you know where to look for.

When does it happen?
when you have a state with an action that uses a game object variable, and then you delete that gameobject in the hierarchy. The referenced game object is now "null".

The error in the console is like this:
Code: [Select]
NullReferenceException: Object reference not set to an instance of an object
HutongGames.PlayMaker.Actions.SetUIGradient.OnEnter () (at Assets/Playmaker Custom Actions/Coffee/UI Gradient/SetUIGradient.cs:89)
HutongGames.PlayMaker.FsmState.ActivateActions (System.Int32 startIndex) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:222)
HutongGames.PlayMaker.FsmState.OnEnter () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:192)
HutongGames.PlayMaker.Fsm.EnterState (HutongGames.PlayMaker.FsmState state) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3073)
HutongGames.PlayMaker.Fsm.SwitchState (HutongGames.PlayMaker.FsmState toState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3012)
HutongGames.PlayMaker.Fsm.UpdateStateChanges () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2945)
HutongGames.PlayMaker.Fsm.DoTransition (HutongGames.PlayMaker.FsmTransition transition, System.Boolean isGlobal) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2982)
HutongGames.PlayMaker.Fsm.ProcessEvent (HutongGames.PlayMaker.FsmEvent fsmEvent, HutongGames.PlayMaker.FsmEventData eventData) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2508)
HutongGames.PlayMaker.Fsm.SendEventToFsmOnGameObject (UnityEngine.GameObject gameObject, System.String fsmName, HutongGames.PlayMaker.FsmEvent fsmEvent) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2913)
HutongGames.PlayMaker.Fsm.Event (HutongGames.PlayMaker.FsmEventTarget eventTarget, HutongGames.PlayMaker.FsmEvent fsmEvent) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2649)
HutongGames.PlayMaker.Fsm.Event (HutongGames.PlayMaker.FsmEventTarget eventTarget, System.String fsmEventName) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2591)
HutongGames.PlayMaker.Actions.SendEventByName.OnEnter () (at Assets/PlayMaker/Actions/StateMachine/SendEventByName.cs:37)
HutongGames.PlayMaker.FsmState.ActivateActions (System.Int32 startIndex) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:222)
HutongGames.PlayMaker.FsmState.OnEnter () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:192)
HutongGames.PlayMaker.Fsm.EnterState (HutongGames.PlayMaker.FsmState state) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3073)
HutongGames.PlayMaker.Fsm.SwitchState (HutongGames.PlayMaker.FsmState toState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3012)
HutongGames.PlayMaker.Fsm.UpdateStateChanges () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2945)
HutongGames.PlayMaker.Fsm.DoTransition (HutongGames.PlayMaker.FsmTransition transition, System.Boolean isGlobal) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2982)
HutongGames.PlayMaker.Fsm.ProcessEvent (HutongGames.PlayMaker.FsmEvent fsmEvent, HutongGames.PlayMaker.FsmEventData eventData) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2508)
HutongGames.PlayMaker.Fsm.SendEventToFsmOnGameObject (UnityEngine.GameObject gameObject, System.String fsmName, HutongGames.PlayMaker.FsmEvent fsmEvent) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2913)
HutongGames.PlayMaker.Fsm.Event (HutongGames.PlayMaker.FsmEventTarget eventTarget, HutongGames.PlayMaker.FsmEvent fsmEvent) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2649)
HutongGames.PlayMaker.Actions.SendEvent.OnEnter () (at Assets/PlayMaker/Actions/StateMachine/SendEvent.cs:42)
HutongGames.PlayMaker.FsmState.ActivateActions (System.Int32 startIndex) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:222)
HutongGames.PlayMaker.FsmState.OnEnter () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:192)
HutongGames.PlayMaker.Fsm.EnterState (HutongGames.PlayMaker.FsmState state) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3073)
HutongGames.PlayMaker.Fsm.SwitchState (HutongGames.PlayMaker.FsmState toState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3012)
HutongGames.PlayMaker.Fsm.UpdateStateChanges () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2945)
HutongGames.PlayMaker.Fsm.DoTransition (HutongGames.PlayMaker.FsmTransition transition, System.Boolean isGlobal) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2982)
HutongGames.PlayMaker.Fsm.ProcessEvent (HutongGames.PlayMaker.FsmEvent fsmEvent, HutongGames.PlayMaker.FsmEventData eventData) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2508)
HutongGames.PlayMaker.Fsm.SendEventToFsmOnGameObject (UnityEngine.GameObject gameObject, System.String fsmName, HutongGames.PlayMaker.FsmEvent fsmEvent) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2913)
HutongGames.PlayMaker.Fsm.Event (HutongGames.PlayMaker.FsmEventTarget eventTarget, HutongGames.PlayMaker.FsmEvent fsmEvent) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2649)
HutongGames.PlayMaker.Actions.SendEvent.OnEnter () (at Assets/PlayMaker/Actions/StateMachine/SendEvent.cs:42)
HutongGames.PlayMaker.FsmState.ActivateActions (System.Int32 startIndex) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:222)
HutongGames.PlayMaker.FsmState.OnEnter () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:192)
HutongGames.PlayMaker.Fsm.EnterState (HutongGames.PlayMaker.FsmState state) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3073)
HutongGames.PlayMaker.Fsm.SwitchState (HutongGames.PlayMaker.FsmState toState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3012)
HutongGames.PlayMaker.Fsm.UpdateStateChanges () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2945)
HutongGames.PlayMaker.Fsm.UpdateState (HutongGames.PlayMaker.FsmState state) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3089)
HutongGames.PlayMaker.Fsm.Update () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2232)
PlayMakerFSM.Update () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/PlayMakerFSM.cs:624)

Literally the only clue that it gives you is the action that is being used (in this case: SetUIGradient.cs). But there is no reference to the FSM or the state and you can't click on the error either so it would take you to the corresponding Playmaker FSM.

What is the best way to track down that error if you don't know where to look for?

And what's the reason why the error in the console can't output the FSM/State where the gameobject is missing?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7618
    • jinxtergames
Re: How to track down NullReferenceException
« Reply #1 on: March 01, 2023, 10:38:22 AM »
Hi.
on the action window you can try to refresh, them rightclick on the action and click 'find..' to see where the actions are used, you should be able to click on them


another way you could try is to add a    Debug.LogWarning
Code: [Select]
Debug.LogWarning("Fsm Name : "+ Fsm.Name + " / Fsm State : " + Fsm.ActiveStateName + " / Game Object Name : " + Fsm.GameObjectName, Fsm.GameObject);just before line 89 on the SetUIGradient.cs

it should show in the console each time its triggered (with or without the error.
when you get the error loon on the warning just before the error and it should show the data from that fsm

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 256
Re: How to track down NullReferenceException
« Reply #2 on: March 01, 2023, 01:59:03 PM »
The Debug Log Warning is interesting, but what with default actions from Playmaker?

Would there be a possibility to have playmaker actually report the same just when it's null?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7618
    • jinxtergames
Re: How to track down NullReferenceException
« Reply #3 on: March 02, 2023, 12:41:47 AM »
You can edit the script and use a if statement

for example :
Code: [Select]
if(variable.isNone || variable.Value == null)
{
// Do Something (for example Debug)
}
this will check if the FsmVariable is set to none or the value in it is null

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 256
Re: How to track down NullReferenceException
« Reply #4 on: March 02, 2023, 12:43:59 PM »
Oh this is good stuff. I will try this next time. Appreciate the help.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7618
    • jinxtergames
Re: How to track down NullReferenceException
« Reply #5 on: March 03, 2023, 04:13:10 AM »
Warning!!!
You are starting to learn C# coding now.....There's no way back!!!  ;D ;D ;D

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 256
Re: How to track down NullReferenceException
« Reply #6 on: March 03, 2023, 02:58:37 PM »
Haha yes, I love doing c# like this: experimenting, doing some little things, then suddenly writing something bigger.

It will still take a life to really learn c# entirely though. And most probably my brain won't accept it and it never will really click... we'll see.

Playmaker is king though for most of what I do. :)

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7618
    • jinxtergames
Re: How to track down NullReferenceException
« Reply #7 on: March 04, 2023, 09:31:52 AM »
Hi.
I actually learned C# thanks to playmaker.