The stack trace requires something, but I don't understand what

Hi all! Recently I decided to make myself a calculator and for this I used the guide “Let’s create a calculator in Godot 4 - Part 2” then I encountered that stack 14 threw an error and at the same time the “stack trace” console appeared and the calculator window turned black.
If you need access to the project in addition to the code, then send it via telegram @Fidence_official
can you help?

`extends Control

@onready var prev_work_area = $Screen/ColorRect/AllComponentsContainer/DisplayContainer/MarginContainer/VBoxContainer/PrevWorkArea
@onready var work_area = $Screen/ColorRect/AllComponentsContainer/DisplayContainer/MarginContainer/VBoxContainer/WorkArea
@onready var all_buttons = $Screen/ColorRect/AllComponentsContainer/AllButtons

var has_been_used := false
var first_number : float
var second_number : float
var operator := “”
var decimal_allowed := true

func _ready():
for buttons in all_buttons.get_children():
for btn in buttons.get_children():
if btn.name.is_valid_int():
btn.pressed.connect(Callable(self, “_number_buttons”).bind(btn))

func _number_buttons(btn):
if not has_been_used:
work_area.text = btn.name
has_been_used = true
decimal_allowed = true
else:
work_area.text += btn.name

func _on_equal_btn_pressed():
has_been_used = false
var result : float
second_number = work_area.text.to_float()
match operator:
“+”:
result = first_number + second_number
“-”:
result = first_number - second_number
“*”:
result = first_number * second_number
“/”:
result = first_number / second_number
if prev_work_area.text.is_empty():
prev_work_area.text = str(second_number)
else:
prev_work_area.text = str(first_number) + " " + operator + " " + str(second_number)
work_area.text = str(snappedf(result, 0.0000000001))

decimal_allowed = true

func _on_addition_btn_pressed():
has_been_used = false
first_number = work_area.text.to_float()
operator = “+”
prev_work_area.text = str(first_number) + " " + operator
decimal_allowed = true

func _on_subtraction_btn_pressed():
has_been_used = false
first_number = work_area.text.to_float()
operator = “-”
prev_work_area.text = str(first_number) + " " + operator
decimal_allowed = true

func _on_multiplication_btn_pressed():
has_been_used = false
first_number = work_area.text.to_float()
operator = “*”
prev_work_area.text = str(first_number) + " " + operator
decimal_allowed = true

func _on_division_btn_pressed():
has_been_used = false
first_number = work_area.text.to_float()
operator = “/”
prev_work_area.text = str(first_number) + " " + operator
decimal_allowed = true

func _on_delete_btn_pressed():
if has_been_used:
if work_area.text.length() >= 2:
work_area.text = work_area.text.left(-1)
else:
decimal_allowed = true
work_area.text = “0”
has_been_used = false

func _on_negate_btn_pressed():
var result : float
first_number = work_area.text.to_float()
result = -first_number
if not has_been_used:
prev_work_area.text = “-” + str(first_number)
else:
prev_work_area.text = str(first_number)

work_area.text = str(result)
has_been_used = true

func _on_clear_btn_pressed():
has_been_used = false
prev_work_area.text = “”
work_area.text = “0”
decimal_allowed = true

func _on_decimal_btn_pressed():
if decimal_allowed:
if has_been_used == true:
work_area.text = work_area.text + “.”
decimal_allowed = false
else:
work_area.text = “0.”
has_been_used = true
decimal_allowed = false`

what does it say?

extends Control

@onready var prev_work_area = $Screen/ColorRect/AllComponentsContainer/DisplayContainer/MarginContainer/VBoxContainer/PrevWorkArea
@onready var work_area = $Screen/ColorRect/AllComponentsContainer/DisplayContainer/MarginContainer/VBoxContainer/WorkArea
@onready var all_buttons = $Screen/ColorRect/AllComponentsContainer/AllButtons

var has_been_used := false
var first_number : float
var second_number : float
var operator := ""
var decimal_allowed := true

func _ready():
	for buttons in all_buttons.get_children():
		for btn in buttons.get_children():
			if btn.name.is_valid_int():
				btn.pressed.connect(Callable(self, "_number_buttons").bind(btn))

func _number_buttons(btn):
	if not has_been_used:
		work_area.text = btn.name
		has_been_used = true
		decimal_allowed = true
	else:
		work_area.text += btn.name

func _on_equal_btn_pressed():
	has_been_used = false
	var result : float
	second_number = work_area.text.to_float()
	match operator:
		"+":
			result = first_number + second_number
		"-":
			result = first_number - second_number
		"*":
			result = first_number * second_number
		"/":
			result = first_number / second_number
	if prev_work_area.text.is_empty():
		prev_work_area.text = str(second_number)
	else:
		prev_work_area.text = str(first_number) + " " + operator + " " + str(second_number)
	work_area.text = str(snappedf(result, 0.0000000001))
	
	decimal_allowed = true

func _on_addition_btn_pressed():
	has_been_used = false
	first_number = work_area.text.to_float()
	operator = "+"
	prev_work_area.text = str(first_number) + " " + operator
	decimal_allowed = true

func _on_subtraction_btn_pressed():
	has_been_used = false
	first_number = work_area.text.to_float()
	operator = "-"
	prev_work_area.text = str(first_number) + " " + operator
	decimal_allowed = true

func _on_multiplication_btn_pressed():
	has_been_used = false
	first_number = work_area.text.to_float()
	operator = "*"
	prev_work_area.text = str(first_number) + " " + operator
	decimal_allowed = true

func _on_division_btn_pressed():
	has_been_used = false
	first_number = work_area.text.to_float()
	operator = "/"
	prev_work_area.text = str(first_number) + " " + operator
	decimal_allowed = true

func _on_delete_btn_pressed():
	if has_been_used:
		if work_area.text.length() >= 2:
			work_area.text = work_area.text.left(-1)
		else:
			decimal_allowed = true
			work_area.text = "0"
			has_been_used = false

func _on_negate_btn_pressed():
	var result : float
	first_number = work_area.text.to_float()
	result = -first_number
	if not has_been_used:
		prev_work_area.text = "-" + str(first_number)
	else:
		prev_work_area.text = str(first_number)
	
	work_area.text = str(result)
	has_been_used = true

func _on_clear_btn_pressed():
	has_been_used = false
	prev_work_area.text = ""
	work_area.text = "0"
	decimal_allowed = true

func _on_decimal_btn_pressed():
	if decimal_allowed:
		if has_been_used == true:
			work_area.text = work_area.text + "."
			decimal_allowed = false
		else:
			work_area.text = "0."
			has_been_used = true
			decimal_allowed = false

i mean what the debugger said about the error at line 14?

my guess now is that the AllButtons node is not yet ready, but you try to get all its children, hence the error of null:

here is the fix for that if that’s the error:

func _ready():
	await get_tree().physics_frame
	for buttons in all_buttons.get_children():
		for btn in buttons.get_children():
			if btn.name.is_valid_int():
				btn.pressed.connect(Callable(self, "_number_buttons").bind(btn))
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.