I have a strange error when executing a timertask, what cause it?
public void tick(long milliseconds) {
ctimer = new Timer();
ctimer.schedule(new TimerTask() {
public void run() {
System.out.println("sec"+sequencer.getMicrosecondPosition()/1000000); }
}, 0, milliseconds);
}
Error message from command prompt:
Exception in thread "Timer-0" java.lang.NullPointerException
at MidiTest$1.run(miditest.java:244)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
I would bet that
sequencer.getMicrosecondPosition()returns aLongand it’s null. The problem happens because autoboxing/unboxing cannot handle nulls.edit
Autoboxing is when java transforms a primitive type to it’s object wrapper, for example
longtoLong. You can read more about this feature in these docs.There’s no recipe to fix this, as it depends on what you want to do. Probably you can replace the body of
run()withwhich will default the position to 0 if getMicrosecondPosition() is null