On my client I am trying to make MIDI files fade out before continuing on to the next MIDI in the sequence, but I've been having trouble with it.
With the code below, it causes a nullpointerException at track.add
, I was wondering what I am doing wrong?
I'm relatively new to Java programming, so please be specific in your answers
public void run() {
active = true
String s = findcachedir();
uid = getuid(s);
if(midiplay)
{
/*
midi = s + savereq;
midiplay = false;
*/
midi = s + savereq;
try {
//System.out.println("Play MIDI " + midi);
if (musicSr != null)
{
musicSr.stop();
musicSr.close();
}
musicSr = null;
musicS = null;
File music = new File(midi);
if(music.exists())
{
musicS = MidiSystem.getSequence(music);
}
for (int k = 0; k < 16; k++) {
musicS.getTracks();
track.add(
new MidiEvent(
new ShortMessage(
ShortMessage.CONTROL_CHANGE,
k,
7,
20),
track.getTicks()));
}
// Create a sequencer for the sequence
musicSr = MidiSystem.getSequencer();
musicSr.open();
musicSr.setSequence(musicS);
musicSr.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
musicSr.start();
} catch (Exception ex) {
ex.printStackTrace();
}
midiplay = false;
}
savereq = null;
}
}
public static Sequencer musicSr = null;
Sequence musicS = null;
` The entire class if you need it for any reason
The variable
track
seems not to be initialized. May be you should do something like this :or if you have getter setters implemented then set the value in
track
before usingadd
method. Something likesetTrack(trackObj);