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));
精彩评论