Godot Version
Godot Engine v4.4.1.stable
Question
I am following a Godot 3 tutorial, and I had to update many of the lines for my current stable version. While I was able to update some lines of codes, whenever I run the game – I get an error Attempt to call function ‘get_as_text’ in base ‘null instance’ on a null instance. How do i fix it?
Here is a code snippet of the code:
func _load_json(name: String) -> Dictionary:
var json = JSON.new()
var file = FileAccess.open("res://", FileAccess.READ)
var content = file.get_as_text()
if not content.error == OK:
return {}
file.close()
return content.result
Hi,
"res://"
is not a valid file path, it’s actually a folder. You should specify a valid full path to a file from your project, something like res://my_file.txt
.
Since your path is invalid, the open
method does not return anything, explaining why file
is null.
Also, if you have other issues with FileAccess.open
, you can also use the get_open_error method to debug what went wrong in your code.
1 Like
Thank you. I fixed the file path so it is now valid. I used the get_open_error method and the warning in the console printed JSON_parser.gd:25 @ _load_json(): Couldn’t open file because: File not found.
1 Like
It cannot open a file for read-only access that doesn’t exist. So create the file. Or better yet, create a save function that makes the file.
1 Like