java - Timed functions throw null pointer exception -
hi creating class in can pass down functions called either periodicity or once off. . problem functions passing down gets called immediately, after when suppose called following error:
java.lang.nullpointerexception @ timedfunction$2.run(timedfunction.java:41) @ java.util.timerthread.mainloop(unknown source) @ java.util.timerthread.run(unknown source)
the base class follow (only relivant sections):
import java.util.concurrent.callable; ... public timedfunction () {} public void addsingleevent (callable func, int seconds){ //convert seconds miliseconds int time = seconds * 1000; //create new timer new java.util.timer().schedule( new java.util.timertask() { @override public void run() { try { func.call(); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } }, time ); }
i testing class looks this:
public static void main(string[] args) { // todo auto-generated method stub timedfunction tm = new timedfunction(); tm.addsingleevent(helloworld(), 5); tm.addrepeatedevent(dataworld(), 1); } private static callable helloworld() { system.out.print("hello world!"); system.out.print(" "); return null; }
check helloworld
method - it's returning null!
i think needs like
private static callable helloworld() { return new callable<string>() { return "hello world"; } }
if had guess, think you're getting confused how use lambas. if that's case, syntax wrong, should this:
tm.addsingleevent(() -> "hello world", 5); tm.addrepeatedevent(() -> "data world", 1);
Comments
Post a Comment