pgonzaleznetwork pgonzaleznetwork - 8 months ago 49
Java Question

InvalidMidiDataException: command out of range

I have this app from Head First Java

import javax.sound.midi.*;

public class MiniMiniMusicApp{
public static void main(String[] args){
MiniMiniMusicApp mini = new MiniMiniMusicApp();;
public void play(){
Sequencer player = MidiSystem.getSequencer();;
Sequence seq = new Sequence(Sequence.PPQ,4);
Track track = seq.createTrack();
ShortMessage a = new ShortMessage();
MidiEvent noteOn = new MidiEvent(a,1);

ShortMessage b = new ShortMessage();
MidiEvent noteOff = new MidiEvent(b,16);


} catch(Exception ex){

And this throws the following exception at runtime

javax.sound.midi.InvalidMidiDataException: command out of range: 0x72
at javax.sound.midi.ShortMessage.setMessage(
at MiniMiniMusicApp.main(

I read some docs on setMessage and it seems the exception is thrown when you pass an invalid MidiMessage but I'm simply following the example from the book. I've read on other forums and it appears that this code works for other people.

Any idea on what is causing the issue? Can you try running this on your end and see if it works? At least that'll tell me if it's something on my environment.

Answer Source

You probably want to send a NoteOn message (144, not 114). The class ShortMessage contains constants which prevent such mistakes. You could use it like this:

import static javax.sound.midi.ShortMessage.*;

a.setMessage(NOTE_ON, 1, 44, 100);

Such code is probably easier to read for most people than directly using the numbers.