InvocationTargetException和NullPointerException JavaFX

时间:2016-05-09 19:12:20

标签: java javafx nullpointerexception

我正在使用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方法而获得异常,或者是否由其他原因引起。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

SceneBuilder允许某人编辑FXML文件。这些文件描述了一个视图,一个界面,就像HTML文件一样,它可以用于网页。

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

Here一个如何做的例子。

答案 1 :(得分:1)

首先使用SceneBuilder让您的生活更轻松。它将为您创建FXML。

其次使用SceneBuilder&#39; On Action&#39;代码部分中的字段直接命名按钮在控制器中的处理方法,而不会使用匿名类作为参数进行笨拙的setOnMouseClicked()调用。它应该在FXML中看起来像这样:

<Button onAction="#onBtnClicked" text="Text on button" />

在控制器中:

@FXML
void onBtnClicked(ActionEvent event) {
   // your handling code goes here
}

然后FXMLLoader可用于从* .fxml加载javaFx Node并将所有@FXML带注释的字段注入到UI控制器的实例中。

URL fxmlLocation = new URL("url to your fxml");
FXMLLoader loader = new FXMLLoader(fxmlLocation);
PremiumCalculator controller = new PremiumCalculator();
loader.setController(controller);
// now load and inject  
Node javafxNode = loader.load();

javafxNode添加到场景中,使其成为可见场景图的一部分。这应该可以解决问题。此外,您在控制器中的代码将更简单,更容易理解。