Ravekitty
Ravekitty

Reputation: 77

NullPointerException and NoSuchElementException

I get this error

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)

from these methods

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();
    }
}

And I honestly don't see what the problem is, can anyone help me out?

Any hints in the right direction would be appreciated

Here's the entire class if you need it, http://pastebin.com/0hVw83zn

Upvotes: 0

Views: 1846

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279880

File#listFiles() can return null. See the javadoc

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

Your catch statement swallows the NullPointerException and then

player.start_playing();

tries to take from the Queue and fails.

Upvotes: 4

Related Questions