1

We have a gui that we use to set teams, and we assign 2 drivers to each team, plus 2 engines, 1 mechanic and 1 aerodinamic plus 1 strategiest. In our gui we set them all and we initialize the value for each of the above. Our GUI is some sort of racing simulation and it is supposed to return the winner.

However everytime we compile the GUI , we get this error:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException at models.Champ.calcRisk(Champ.java:75) at models.Champ.simtRace(Champ.java:102) at views.RScene.start(RScene.java:317) at views.RScene.lambda$0(RScene.java:107) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) at javafx.event.Event.fireEvent(Event.java:198) at javafx.scene.Node.fireEvent(Node.java:8413) at javafx.scene.control.Button.fire(Button.java:185) at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182) at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96) at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89) at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) at javafx.event.Event.fireEvent(Event.java:198) at javafx.scene.Scene$MouseHandler.process(Scene.java:3757) at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485) at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417) at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416) at com.sun.glass.ui.View.handleMouseEvent(View.java:555) at com.sun.glass.ui.View.notifyMouse(View.java:937) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) at java.lang.Thread.run(Unknown Source)

this is really wierd because we dont know how and why something is zero, we debuged it and it seems everything works fine (by debugging) however when you just run the main method we get all of these following error.

we have a method

public double calcRisk(TDriver td) {
        double risk = MIN_RISK;
        risk += td.getCar().getSetupStrategy();
        risk += (100 - td.getTeam().getStrategist().getProficiency());
        return risk;
    }

and also in out method to simulate the race we have:

double crashRisk = calcRisk(td);

and this method:
private void beginRace() {

        int raceNumber = Interact.game.getChamp().getNumber();
        System.out.println("RN " + raceNumber);

        StartInterac.game.getCham().simulateNextRace();

        rScene();

    }

and the next error is in the following method:

private void raceStartScene() {
        .....rest of the code.......
        raceBtn.setOnAction(e -> {
            beginRace();
        });

        ......rest of the code......
    }

EDIT:

we have amethod geeTeam which is located in a class called DriverPerTeam

we put a check for this method:

public double calcRisk(TDriver td) {
System.out.println("---check----");
            System.out.println("1 "+td.getTeam());
            System.out.println("2 "+td.getTeam().getStrategist());
            System.out.println("3 "+td.getTeam().getStrategist().getProficiency());
        System.out.println("-------------");
        double risk = MIN_RISK;
        risk += td.getCar().getSetupStrategy();
        risk += (100 - td.getTeam().getStrategist().getProficiency());
        return risk;
    }

and now it returns answer like this:

    ---check----
    1 models.Team@627f66d0
    2 models.Strategist@1c00c341
    3 71.0
    -------------
    ---check----
    1 models.Team@627f66d0
    2 models.Strategist@1c00c341
    3 71.0
    -------------
    ---check----
    1 null
    Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
      at models.Champ.calcRisk(Champ.java:76)
      at models.Champ.simRace(Champ.java:108)
      at views.RScene.beginRace(RScene.java:317)
.
.
.
.
---check----
1 models.Team@627f66d0
2 models.Strategist@1c00c341
Exception in thread "JavaFX Application Thread" 3 71.0
-------------
---check----
1 models.Team@627f66d0
2 models.Strategist@1c00c341
3 71.0
-------------
---check----
1 null
java.lang.NullPointerException
  at models.Champ.calcRisk(Champ.java:76)
  at models.Champ.simRace(Champ.java:108)
  at views.RScene.beginRace(RScene.java:317)
.
.
.
.
.
.

anyone can help me in this situation??

S. N
  • 3,456
  • 12
  • 42
  • 65

1 Answers1

0

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?

pdem
  • 3,880
  • 1
  • 24
  • 38