What does null instance mean.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By TheNewGrant

This is less of a personal question but more of what I discovered and what could help anyone in need.

Very often I run into the can’t do blank on base Null Instance which was a really big wall in my game making, but after some searching on the Godot Docs in the node section it states under the get_node() function that a null instance is where Godot cannot find a node. Using that knowledge if you run into that error you should first check to see if you’re correctly calling the node (If it is a child of a node like…)

-KinematicBody2D
     -Area2D
         -CollisionShape2D

make sure you are using $Area2D/CollisionShape2D or get_node(“Area2D/CollisionShape2D”)
to call it correctly. That may be what fixes your errors.

I hope this knowledge is useful because I really found it handy.

:bust_in_silhouette: Reply From: Zylann

In the case of get_node or $SomeNode, a null instance error following that indeed means the node wasn’t found. But the null instance error itself only means you are trying to call a method on a variable which contains null. For example, in obj.do_this(), if obj is null, Godot cannot call do_this(). This can happen in many other cases not involving nodes.

The debugger will stop on those null accesses because there is no recovery possible, but the downside is that the origin of that null may have run long before, which is not immediately obvious.
Functions that can return null in case of an error don’t stop the debugger. However, they may log an error instead, which you can see in the console log, with the call stack attached most of the time. This is what you should look for, in case you run into a null error.
The debugger doesn’t stop because the program can decide to keep running: setting a variable to null isn’t preventing GDScript from working. But trying to call a method on null does.

That said… I wish there was a break on error option in Godot so that the debugger actually stops the game pointing at the origin of errors as they occur, especially if the origin is a script.