0

在我的客户端上,我试图让 MIDI 文件淡出,然后再继续处理序列中的下一个 MIDI,但我一直遇到问题。

使用下面的代码,它会导致 nullpointerException track.add,我想知道我做错了什么?

我对Java编程比较陌生,所以请具体回答

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;

` 整个班级,如果您出于任何原因需要它

http://pastebin.com/6C7GNnib

4

1 回答 1

1

该变量track似乎没有被初始化。也许你应该做这样的事情:

private Track track = new Track();

或者如果您实现了 getter setter,则track在使用add方法之前设置值。就像是setTrack(trackObj);

于 2013-09-25T18:48:27.483 回答