gpt4 book ai didi

java - NullPointerException 和 NoSuchElementException

转载 作者:行者123 更新时间:2023-12-01 18:42:34 24 4
gpt4 key购买 nike

我收到此错误

java.lang.NullPointerException
at MidiPlayer.add_midi_files(MidiPlayer.java:59)
at MidiPlayer.main(MidiPlayer.java:51)
Songs added!
Starting to play
Exception in thread "main" java.util.NoSuchElementException
at java.util.LinkedList.removeFirst(LinkedList.java:268)
at java.util.LinkedList.remove(LinkedList.java:683)
at MidiPlayer.play_next_song(MidiPlayer.java:31)
at MidiPlayer.start_playing(MidiPlayer.java:47)
at MidiPlayer.main(MidiPlayer.java:54)

通过这些方法

public static void main(String[] args){
Queue<String> song_queue = new LinkedList<String>();
MidiPlayer.add_midi_files(song_queue,new File( "C:\\Users\\Kyle\\Desktop\\clieent\\Client\\cache\\Music\\runescape-7th realm.mid"));
System.out.println("Songs added!");
MidiPlayer player = new MidiPlayer(song_queue);
player.start_playing();
};

public static void add_midi_files(Queue<String> queue, File top_dir){
try {
for (File f: top_dir.listFiles())
if (f.isDirectory())
add_midi_files(queue, f);
else if (f.isFile() && f.getName().matches(".*midi?"))
{
System.out.println(f);
queue.add(f.getAbsolutePath());
}
else
{}
//System.out.printf("%s doesn't match!", f.getName());
} catch (NullPointerException e) {
e.printStackTrace();
}
}

老实说,我不明白问题出在哪里,有人可以帮助我吗?

任何正确方向的提示将不胜感激

如果您需要的话,这是整个类(class),http://pastebin.com/0hVw83zn

最佳答案

File#listFiles() 可以返回 null。请参阅javadoc

Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

您的catch语句吞下NullPointerException,然后

player.start_playing();

尝试从队列中获取数据但失败。

关于java - NullPointerException 和 NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19325330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com