Can't call "get_next" on a null value.

Hello. I’ve been bashing my head against this code for a while now and finally decided it was time to ask for help. I’m trying to make it so that music plays in a level using a global so that it continues between scenes. Currently I’m trying to make the menu play music, but the code keeps telling me I can’t call “get_next” on a null value. Does anyone have any idea why it’s not working? Thank you.

Also sorry that my code is so scuffed. I’m a relatively inexperienced developer.

extends AudioStreamPlayer

const TITLEMUSIC = preload("res://music/titlemusic.mp3")
const NONE = preload("res://music/none.mp3")

@onready var level = ""
@onready var levelsPath = "res://scenes/levels/"
@onready var menuPath = "res://scenes/ui/"

func _ready() -> void:
	var extension = "tscn"
	getFilePathsByExtension(level, extension, true)
	
func getFilePathsByExtension(path: String, extension: String, recursive: bool = true):
	path = scene_file_path.get_basename()
	var noPath = ""
	
	if path.get_base_dir() == menuPath:
		path = menuPath
	else:
		path = noPath
	
	extension = "tscn"
	
	var dir := DirAccess.open(path)
	
	var filePaths := []
	var fileName := dir.get_next()
	
	level = fileName
	
	while fileName != "":
		if dir.current_is_dir():
			if recursive:
				var dirPath = dir.get_current_dir() + "/" + fileName
				filePaths += getFilePathsByExtension(dirPath, extension, recursive)
				
			fileName = dir.get_next()
			
	return filePaths

func PlayMusic(music: AudioStream, volume = -19.0):
	if stream == TITLEMUSIC:
		return
	if stream == NONE:
		return
	
	var extension = "tscn"
		
	if getFilePathsByExtension(level.get_base_dir(), extension, true) == getFilePathsByExtension(menuPath, extension, true):
		stream = TITLEMUSIC
		stream.loop = true
		volume_db = volume
		play()
	
	if getFilePathsByExtension(level.get_base_dir(), extension, true) == getFilePathsByExtension(levelsPath, extension, true):
		stream = NONE
		stream.loop = true
		volume_db = volume
		play()
	
func PlayMusicMenu():
	PlayMusic(TITLEMUSIC)

That means DirAccess.open(path) is returning null, this function return null when the path can’t be opened for any reason (invalid path, locked, no autorization, etc). You need to check which path you’re passing for this function and which error is happening with DirAccess.get_open_error(). Do the following code and show what this prints.

extension = "tscn"
	
var dir := DirAccess.open(path)

# Failed to open, check the path used and the error
if dir == null:
	print("Failed to open '%s' path with error: %s" %[path, str(DirAccess.get_open_error())])
	return
	
var filePaths := []
var fileName := dir.get_next()
1 Like

I get the error:

Failed to open '' path with error: 31

Any idea what the 31 means? I assume it’s a numbered error code but I can’t find what it actually means anywhere online.

This is from the Error enumerator in the @GlobalScope class, means “invalid parameter” because you’re passing an empty string as path, you need to check your code to pass the correct path.