# Cannot call method '…' on a null value in Godot 4

The variable is typed, as a Node2D for example, and holds null when the method is called. player.hide() with var player: Node2D never assigned stops with Cannot call method 'hide' on a null value. Assign it first, usually with @onready var player: Node2D = $Player, or check it is not null.

Source: https://flockbay.com/godot/cannot-call-method-on-a-null-value/ · Updated 2026-10-30

## What Godot is checking

When the variable's class is known, the compiler calls the method directly instead of looking it up at run time. Before the call the VM checks that the object is not null, and stops with this message when it is.

Through an untyped variable the same mistake prints Invalid call. Nonexistent function 'hide' in base 'Nil'.

## Reproduce it

A typed Node2D variable that is never assigned, then called.

```
extends Node

var player: Node2D

func _ready() -> void:
	player.hide()
	print("after")

```

## What Godot 4.7.2 prints

```
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org

SCRIPT ERROR: Cannot call method 'hide' on a null value.
   at: _ready (res://answers/cannot-call-method-on-a-null-value/repro.gd:6)
   GDScript backtrace (most recent call first):
       [0] _ready (res://answers/cannot-call-method-on-a-null-value/repro.gd:6)
```

## Fix it

Assign it from the scene, which here has a Player child, and guard calls that may come early.

```
extends Node

@onready var player: Node2D = $Player

func _ready() -> void:
	if player:
		player.hide()
	print("visible: ", player.visible)

```

## What the fixed script prints

```
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org

visible: false
```

## Run this fix with your agent

Flockbay is the Godot editor with an AI agent built in. Paste this into Flockbay's chat, or into Claude Code or Codex connected to Flockbay over MCP: the agent finds the cause in your project, applies the fix, runs the game headless and reads Godot's output to check it.

```
My Godot 4 project prints this error: "Cannot call method 'hide' on a null value.". Find where it comes from and fix it. The variable is typed, as a Node2D for example, and holds null when the method is called. player.hide() with var player: Node2D never assigned stops with Cannot call method 'hide' on a null value. Assign it first, usually with @onready var player: Node2D = $Player, or check it is not null. Then run the game headless and check that Godot's output no longer shows it.
```

## An exported node never set in the Inspector

@export var player: Node2D stays null until you pick a node for it in the Inspector. Every call on it fails this way until you do.

## A get_node() that found nothing

$Player returns null when the path is wrong, after printing Node not found. Fix the path, or use a unique name (%Player).

## The same error in earlier Godot versions

Godot 3.6 printed it as: Attempt to call function 'hide' in base 'null instance' on a null instance. The cause and the fix are the same.

## Verified on Godot 4.7.2

Both scripts on this page were run headless on Godot 4.7.2 (4.7.2.stable.mono.official.ed1daf0bf) on 18 October 2026, and the output above is what Godot printed, unedited.

Where the check lives in Godot's source: [gdscript_vm.cpp line 496, the message](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L496).

## Questions

### What is the difference from Nonexistent function in base 'Nil'?

Only the variable's type. A typed variable gives Cannot call method on a null value; an untyped one gives Nonexistent function in base 'Nil'. Both mean the variable is null.

### Does if player: work for a freed node?

No. A freed node is not null in the same way; use is_instance_valid(player) when the node may have been freed.

Download Flockbay free: Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot. https://flockbay.com/download/

Related: [Invalid call. Nonexistent function '…' in base 'Nil' in Godot 4](/godot/nonexistent-function-in-base-nil/), [Cannot call method '…' on a previously freed instance in Godot 4](/godot/cannot-call-method-on-a-previously-freed-instance/), [Invalid access to property or key 'position' on a base object of type 'null instance' in Godot 4](/godot/invalid-access-to-property-or-key-on-null-instance/), [Invalid access to property or key 'position' on a base object of type 'Nil' in Godot 4](/godot/invalid-access-to-property-or-key-on-nil/), [Assertion failed in Godot 4](/godot/assertion-failed/), [Every verified Godot 4 answer](/godot/), [How do I see Godot’s output and errors in Flockbay?](/docs/how-do-i-see-godots-output-in-flockbay/)
