trying to fix java Exception in thread “JavaFX Application Thread” java.lang.NullPointerException

If you look at the stacktrace the error is at RScene:107 and the caller is javafx. So the problem is not in the beginRace method and its caller raceStartScene. My guess is at this line (seems to be an attribute):

double crashRisk = calcRisk(td);

you call implicitly calcRisk(td) when creating this object, you have no guarantee that td is not null.

My good practice advise would be to never initialize variable at the declaration, but do it in an explicit constructor, and then check for every nullity possible for a fail fast behavior.

public class YourClass{

double crashRisk;

public YourClass(){
 TDriver td = ...//get td
 if(td == null){
   throw new YourExplicitException();
 }
 crashRisk = calcRisk(td);
}

PS: We may need the context of this line ” double crashRisk = calcRisk(td);” could you provide more source?

Leave a Comment