1
I am wanting to make an application that checks the price of a product every minute.
In my Code test this as follows:
package sample;
import javafx.stage.Stage;
import java.awt.*;
import java.util.Timer;
import java.util.TimerTask;
import static sample.Controller.updateTicker;
public class MyTask{
public Label helloWorld;
public void criarTimer(Stage primaryStage) {
int segundos = 10;
int segundosParaComecar = 0;
int segundosParaCapturar = segundos*1000;
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
public void run() {
helloWorld.setText(updateTicker());
}
};
timer.schedule(timerTask, segundosParaComecar, segundosParaCapturar);
}
}
But I’m having the following Error log and I don’t know how to fix it:
Exception in thread "Timer-0" java.lang.Nullpointerexception at sample.Mytask$1.run(Mytask.java:23) at java.util.Timerthread.mainLoop(Timer.java:555) at java.util.Timerthread.run(Timer.java:505)
And error occurs when you set the new String to the Label. The method updateTicker()
is returning String as expected.
The
public Label helloWorld
is being instantiated and you are assigning a reference to the instance ofMyTask
when the instance? This seems to be the variable that is null.– Wtrmute
She’s instantiated as soon as I carry mine
FXML
. When the Access in which you want another class I do not have this error. Only when I put it within the Methodrun()
– Muttley
Can the timer be instantiated before the FXML load? Try to put in
run()
if (helloWorld != null) helloWorld.setText(updateTicker());
– Wtrmute
It really was null, I changed the moment I call my Method
criarTask()
and it worked. Thank you– Muttley