0

I am building a midi piano in java. In this case, I want to play pitches ,record pitches and playback them.

Then I used java thread to add pitch to queue and add delay to queue. I used thread object to these process and call it in constructor. After the call this constructor it gives NullPointException.

Can you help me found out why?

public PianoPlayer() throws MidiUnavailableException{
     queue = new LinkedBlockingQueue<NoteEvent>();  
     delayQueue = new LinkedBlockingQueue<NoteEvent>();
     machine = new PianoMachine(this);
     processQueue.start();
     processDelayQueue.start();
}

Thread processDelayQueue = new Thread() {
    public void run(){
        while(true){
            if(queue.isEmpty()){
            }
            else{
                try {
                    NoteEvent e=queue.take();
                    midi.Midi.wait(100);
                    queue.put(e);            
                } catch (InterruptedException ex) {
                    Logger.getLogger(PianoPlayer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
   }
};
dWinder
  • 11,597
  • 3
  • 24
  • 39

1 Answers1

0

Check if processQueue and processDelayQueue are not null. NullPointerException is usually caused when you try to call a method or get a value from a variable in an object that is null.

Such as

MyClass c = null;
// NullPointerException
String name = c.getName();

Possible Fix

MyClass c = null;
if (c == null) {
    // initialize
    c = new MyClass();
}
// This will work
String r = c.getName();

You need to always have a null check because it is very common to get a null variable.

BrokenEarth
  • 128
  • 1
  • 10