Godot invalid get index 'null' (on base 'dictionary')

Godot Version: 3.5.1

Hey Everyone

Build some 2D Pixel RPG and when i collect an Item and will open the Inventory the game crashes with the error:
line: var stack_size = int(JsonData.item_data[item_name][“StackSize”])
error: invalid get index ‘null’ (on base ‘dictionary’)

Here is the Code:

extends Node2D

var item_name
var item_quantity

func _ready():
var rand_val = randi() % 3
if rand_val == 0:
item_name = “Muschel1”
elif rand_val == 1:
item_name = “Seestern”
item_name = “Iron Sword”

func set_item(nm, qt):
item_name = nm
item_quantity = qt
$TextureRect.texture = load(“res://UI/Slot_Items/” + str(item_name) + “.png”)

var stack_size = int(JsonData.item_data[item_name]["StackSize"])
if stack_size == 1:
	$Label.visible = false
	$Label.visible = true
	$Label.text = String(item_quantity)

func add_item_quantity(amount_to_add):
item_quantity += amount_to_add
$Label.text = String(item_quantity)

func decrease_item_quantity(amount_to_remove):
item_quantity -= amount_to_remove
$Label.text = String(item_quantity)

i hope you can help me


From the error I think your item_name is null. Double check all the places where you are calling the function set_item. The item_name argument could be also a string (the error would look the same)

the item_name is everytime a string.
function set_item is this only the one position where i call this.

In the line with the error i will check in the json file how is stack size defined for the respective item in the list.

the enty in the json file list shows like that:
“Iron Sword”: {
“ItemCategory”: “Sword”,
“ItemAttack”: 3,
“ItemSpeed”: 0.75,
“StackSize”: 1,
“Description”: “Quite a rusty sword, but should be able to get the job done.”

i will get the number 1 from “StackSize”

Try printing the function argument. I think it’s going to be null

func set_item(nm, qt):
	item_name = nm
	item_quantity = qt
	# ...

yes you’re right, its null.

how can i fix this?

in the line: $TextureRect.texture = load(“res://UI/Slot_Items/” + str(item_name) + “.png”)
I need the string of item_name to load the right Item from the folder of Items

And at the line:
var stack_size = int(JsonData.item_data[item_name][“StackSize”])
I only need the property StackSize from the Item in Integer.

You are calling the function set_item somewhere in your project and the first argument you are passing it is null. It’s probably a variable. So search your entire project for set_item usages and look for the bug.

Good luck