Attempt to call function 'get_nodes_in_group' in base 'null instance' on a null instance.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robster
:warning: Old Version Published before Godot 3 was released.

Hi all,

I am making an arkanoid clone for learning. I have this line which appears to be called when I press escape, to leave the current scene and load the main menu:

if get_tree().get_nodes_in_group("balls").size() <= 1:
	for ball in get_tree().get_nodes_in_group("balls"):
		if ball:
            #do stuff here 

I get the following error:
Attempt to call function 'get_nodes_in_group' in base 'null instance' on a null instance.

There are definitely ball/s on screen when I press escape. The code above is within a function called:

func _on_Timer_timeout():

So I can’t see even why the timer would be triggered and time out.

I’m a little confused. Can anyone see or think of anything that I’m missing?

Thanks so much…

:bust_in_silhouette: Reply From: timoschwarzer

It seems like your node is not inside the scene tree, therefore get_tree() returns null. You might want to check this before running your code with is_inside_tree().

Thank you! That’s a great little function. I can already see other ways I can use this. Much appreciated. Problem now solved.

Robster | 2017-07-24 23:12

How could you fix that problem, please? I’m acrossing a similar issue

EmmanFK9503 | 2019-12-06 15:15

Hi there, I used is_inside_tree() as per /u/timoschwarzer suggested. I hope that helps

Robster | 2019-12-07 00:47

@Robster I think what he meant is, how did you actually fix the error? I am running into the same issue now.

tmit30 | 2020-08-11 20:47

Indeed. I guess looking back (this was a long time ago) I PROBABLY did something like:

i

f get_tree().get_nodes_in_group("balls").is_inside_tree():
    if get_tree().get_nodes_in_group("balls").size() <= 1:
        for ball in get_tree().get_nodes_in_group("balls"):
            if ball:
                #do stuff here 

This is a guess and the code is backed up in cold storage now but I suspect I was using the function is_inside_tree() to check first if the node was actually inside the scene tree.

Robster | 2020-08-11 23:29

extends Panel

func _ready():
	var commons = get_tree().get_nodes_in_group("commons")		
	get_node("Button").connect("button_down", self, "_on_Button_button_down")
	get_node("Button").connect("button_up", self, "_on_Button_button_up")
	get_node("Button").text = str(commons)

func _on_Button_button_down():
	get_node("Label").text = "Hello!"

func _on_Button_button_up():
	get_node("Label").text = ""

In my case it works only if var commons is in the func _ready()

knazigor | 2022-01-02 19:03