我正在使用SceneBuilder创建一个GUI,以通过文本字段从用户那里获取信息。单击calcPremiumButton按钮时,得到此InvocationTargetException
和堆栈跟踪。
> Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
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:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
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:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
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$149(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 31 more
Caused by: java.lang.NullPointerException
at PremiumCalculator.calculatePremiumAction(PremiumCalculator.java:52)
... 41 more
我已经将
NullPointerException
追溯到setOnMouseClicked
方法。 import java.lang.*;
import java.util.*;
import java.awt.*;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
/*** PremiumCalculator ***/
public class PremiumCalculator extends LifeInsurancePolicy
{
@FXML
private static TextField coverage;
@FXML
private static TextField number;
@FXML
private static TextField lName;
@FXML
private static TextField a;
@FXML
private static TextField fName;
@FXML
private static TextField s;
@FXML
private static TextField pName;
@FXML
private static Label outputLabel;
@FXML
private static Button calcPremiumButton;
@FXML
private static TextField w;
@FXML
private static TextField h;
@FXML
private void calculatePremiumAction()
{
/*** setOnMouseClicked ***/
PremiumCalculator.calcPremiumButton.setOnMouseClicked(new EventHandler<MouseEvent>()
{
/*** handle ***/
public void handle(MouseEvent mouseEvent)
{
/*** Get information from the GUI text boxes ***/
String number = PremiumCalculator.number.getText();
String pName = PremiumCalculator.pName.getText();
String fName = PremiumCalculator.fName.getText();
String lName = PremiumCalculator.lName.getText();
String a = PremiumCalculator.a.getText();
String s = PremiumCalculator.s.getText();
String h = PremiumCalculator.h.getText();
String w = PremiumCalculator.w.getText();
String coverage = PremiumCalculator.coverage.getText();
/*** Convert Strings ***/
int num = Integer.parseInt(number);
int ag = Integer.parseInt(a);
double he = Double.parseDouble(h);
double we = Double.parseDouble(w);
double c = Double.parseDouble(coverage);
double lifeInsurance = Double.parseDouble(lifeInsurancePolicyString);
/*** Instantiate a new LifeInsurancePolicy object ***/
LifeInsurancePolicy customer = new LifeInsurancePolicy(num, pName, fName, lName, ag, s, he, we);
/*** Call an instance of the LifeInsurancePolicy object to get the life insurance cost ***/
double lifeInsuranceAmount = customer.calculateLifeInsurance(s, bmi);
/*** Display the life insurance cost ***/
outputLabel.setText("Life Insurance Cost: " + lifeInsuranceAmount);
}
/*** End handle ***/
});
/*** End setOnMouseClicked ***/
}
}
/*** End PremiumCalculator ***/
我不确定我是由于
setOnMouseClicked
方法还是由于其他原因导致了异常。任何帮助将非常感激! 最佳答案
SceneBuilder允许某人编辑FXML文件。这些文件描述了视图,界面,就像HTML文件将其用于网页一样。
问题是您试图将事件放在函数private void calculatePremiumAction()
中的按钮上。
但是此函数已经是一个Event函数(因为它上面有@FXML),您只需要将它与SceneBuilder中的按钮绑定即可。
Here如何执行此操作的示例。