gpt4 book ai didi

java - JavaFX InvokingTargetException 和 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 03:38:43 25 4
gpt4 key购买 nike

我正在使用 SceneBuilder 创建一个 GUI,以通过文本字段从用户获取信息。当我单击 calcPremiumButton 按钮时,我收到此 InvocableTargetException 和堆栈跟踪。

> 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 文件。这些文件描述了一个 View 、一个界面,就像 HTML 文件对网页所做的那样。

问题是您试图在函数 private voidcalculatePremiumAction() 内的按钮上放置一个事件。但这个函数已经是一个事件函数(因为它上面有@FXML),你只需要将它与SceneBuilder中的按钮绑定(bind)即可。

Here应该如何做的示例。

关于java - JavaFX InvokingTargetException 和 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37123645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com