尝试修复线程“JavaFX 应用程序线程”中的 Java 异常 java.lang.NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41876168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
trying to fix java Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
提问by S. N
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.
我们有一个用于设置团队的 gui,我们为每个团队分配 2 名车手,外加 2 名引擎、1 名机械师和 1 名航空动力以及 1 名策略员。在我们的 gui 中,我们将它们全部设置并初始化上述每个的值。我们的 GUI 是某种赛车模拟,它应该返回赢家。
However everytime we compile the GUI , we get this error:
然而,每次我们编译 GUI 时,我们都会收到此错误:
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)
线程“JavaFX 应用程序线程”中的异常 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) 在 com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 在 com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 在 com.sun.javafx.event .BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 在 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.dispatchChainImpl.dispatchcom1Impl.dispatchEvent(14p) .sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在 com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 com.sun.javafx.event.Event.EventUtil.fireEventImpl .java:74) 在 com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) 在 javafx.event.Event.fireEvent(Event.java:198) 在 javafx.scene.Node.fireEvent(Node. java:8413) 在 javafx.scene.control.Button.fire(Button.java:185) 在 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) 在 com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) 在 com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.javacom:80) 在.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 在 com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 在 com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeDispatchEventDispatchBubblingEvent(Composite) :59) 在 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.com.java:56) .javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在 com.sun.javafx.event.EventDispatchChainImpl.java:114 :114) 在 com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 在 com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) 在 javafx.event.Event.fireEvent( Event.java:198) 在 javafx.scene.Scene$MouseHandler.process(Scene.java:3757) 在 javafx.scene.Scene$MouseHandler。在 javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) 在 javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) 在 com.sun.javafx.tk 上访问 $1500(Scene.java:3485)。 com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295) 处的quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381) 处的java.security.AccessController.doPrivileged(Native Method) com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417) 在 com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) 在 com.sun.javafx .tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416) 在 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) 在 java.lang.Thread.run(未知来源)
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.
这真的很奇怪,因为我们不知道某些东西是如何以及为什么为零,我们对其进行了调试,看起来一切正常(通过调试)但是当您只运行 main 方法时,我们会收到所有这些以下错误。
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
我们有一个方法 geeTeam,它位于一个名为 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) . . . . . .
---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??
任何人都可以在这种情况下帮助我?
回答by pdem
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):
如果您查看堆栈跟踪,则错误位于 RScene:107 并且调用方是 javafx。所以问题不在于beginRace 方法及其调用者raceStartScene。我的猜测是在这一行(似乎是一个属性):
double crashRisk = calcRisk(td);
you call implicitly calcRisk(td) when creating this object, you have no guarantee that td is not null.
您在创建此对象时隐式调用 calcRisk(td),您不能保证 td 不为空。
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?
PS:我们可能需要这一行的上下文“double crashRisk = calcRisk(td);” 你能提供更多的来源吗?