所以我正在用 Java 制作一个 MIDI 程序。我有一个缓冲区,这是我用来从缓冲区获取消息并将它们传递给将显示它们的窗口类的线程。
ArrayIndexOutOfBounds
每当我发送只有两个参数的消息时,我都会收到错误消息。我尝试过使用单独的数组和 if 语句,但似乎都不起作用。该错误表明它与 if 语句/第三个参数变量有关。
我使用了 switch 语句和掩码来确定正在接收哪种类型的消息,但是带有两个参数而不是三个参数的消息会引发错误,并且它与数组和参数数量有关。
public class DisplayThread extends Thread {
Buffer bufferObj;
MidiMonitorWindow window = new MidiMonitorWindow("MIDI Devices");
DisplayThread(Buffer bufferObj) {
this.bufferObj = bufferObj;
}
public void run() {
while (true) {
byte[] getMessages = bufferObj.get();
if (getMessages != null) {
int statusValue = getMessages[0] & 0xF0;
int [] paramArray = new int[3];
paramArray[0] = getMessages [0] & 0xFF;
paramArray[1] = getMessages [1] & 0xFF;
if (getMessages[2] != 0){
paramArray[2] = getMessages [2] & 0xFF;
}
switch (statusValue) {
case NOTE_OFF:
System.out.println("Note Off");
window.showNoteOff(paramArray[0], paramArray[1], paramArray[2]);
break;
case NOTE_ON:
System.out.println("Note On");
window.showNoteOn(paramArray[0], paramArray[1], paramArray[2]);
break;
case POLY_PRESSURE:
System.out.println("Poly Pressure");
window.showPolyKeyPressure(paramArray[0], paramArray[1], paramArray[2]);
break;
case CONTROL_CHANGE:
System.out.println("Control Change");
window.showControlChange(paramArray[0], paramArray[1], paramArray[2]);
break;
case PROGRAM_CHANGE:
System.out.println("Program Change");
window.showProgramChange(paramArray[0], paramArray[1]);
break;
case CHANNEL_PRESSURE:
System.out.println("Channel Pressure");
window.showChannelPressure(paramArray[0], paramArray[1]);
break;
case PITCH_BEND:
System.out.println("Pitch Bend");
window.showPitchBend(paramArray[0], paramArray[1], paramArray[2]);
break;
default:
System.out.println("System Message");
window.showSystemExclusive();
break;
}
}
}
}
}
你能提供的任何建议都会很有帮助!