开发者

Unable to read input from jar

开发者 https://www.devze.com 2023-01-21 22:49 出处:网络
When I run java -jar MidiTest.jar, input a MIDI file, it throws: Exception in thread \"main\" java.lang.NullPointerException

When I run java -jar MidiTest.jar, input a MIDI file, it throws:

Exception in thread "main" java.lang.NullPointerException
        at com.sun.media.sound.StandardMidiFileReader.getSequence(StandardMidi
leReader.java:209)
        at javax.sound.midi.MidiSystem.getSequence(MidiSystem.java:802)
        at MidiTest.playMidi(MidiTest.java:56)
        at MidiTest.(MidiTest.java:44)
        at MidiTest.main(MidiTest.java:25)

If I use java MidiTest instead it could play without issue. What wrong with the code? I have already add Main-Class: MidiTest with newline on Manifest file

My code:

        private void playMidi() {
    if(isPlaying.equals("0")) {
        try {
            song = MidiSystem.getSequence(
                getClass().getResource(filename));
            sequencer = MidiSystem.getSequencer();
            sequencer.setSequence(song);
            sequencer.open();
            sequencer.addMetaEventListener(this);
            sequencer.start();
        } catch (InvalidMidiDataExcepti开发者_如何学Pythonon e) {
            System.out.println("Bad midi file: "
                + filename);
            System.exit(1);
        } catch (MidiUnavailableException e) {
            System.out.println("No sequencer available");
            System.exit(1);
        } catch (IOException e) {
            System.out.println("Could not read: "
                + filename);
            System.exit(1);
        }
        displayMidiInfo(filename);
        } else {
        updateTempoFactor(speed);
        }
    }


You don't appear to be checking if the resource you're trying to get is returning something non-null. Specifically:

song = MidiSystem.getSequence(
    getClass().getResource(filename));

is causing this particular problem. There might be a deeper issue, which is that unless the resource represented by filename is actually in the jar, on the Class-Path or in the same directory as the jar file getResource() is not going to find it. If you're trying to access a file anywhere in the general filesystem (not in the jar file) then you should be using File:

song = MidiSystem.getSequence(new File(filename));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号