JavaFX在应用程序启动方法java.lang.reflect.InvocationTargetException中不断获取异常

时间:2020-05-21 10:49:51

标签: java javafx

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.NullPointerException: Children: child node is null: parent = VBox@4b0d9981
    at javafx.graphics/javafx.scene.Parent$3.onProposedChange(Parent.java:542)
    at javafx.base/com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:234)
    at javafx.base/com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:103)
    at javafx.graphics/javafx.scene.layout.VBox.<init>(VBox.java:251)
    at MultiplyApp.start(MultiplyApp.java:34)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Exception running application MultiplyApp

我的代码是




import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import javax.swing.*;  

public class MultiplyApp extends Application {
    private TextArea firstInt;
    private TextArea secondInt;
    private TextArea resultLabel;
    RecursiveMultiply myObj= new RecursiveMultiply();

    public static void main(String[] args) {
        launch(args);
    }
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Recursive Multiply App");
        Label promptLabel= new Label("Enter first positive integer: ");
        Label promptLabel2 = new Label("Enter second positive integer: ");
        firstInt= new TextArea();
        firstInt.setPrefColumnCount(4);
        firstInt.setPrefRowCount(2);
        secondInt= new TextArea();
        secondInt.setPrefColumnCount(4);
        secondInt.setPrefRowCount(2);
        Button calcButton = new Button("Ok");
        HBox hbox= new HBox(10, promptLabel, firstInt, promptLabel2, secondInt);
        VBox vbox= new VBox(10, hbox, resultLabel);
        Scene initial= new Scene(vbox);
        primaryStage.setScene(initial);
        primaryStage.show();
        calcButton.setOnAction(new EventHandler<ActionEvent>()
          {
             @Override
             public void handle(ActionEvent event)
             {
                    try {
                    int Gal = Integer.parseInt(firstInt.getText());
                    int Mil = Integer.parseInt(secondInt.getText());
                    if ((Gal <= 0) || (Mil <= 0))       
                        throw new NegativeDoubleException();
                    int result= myObj.recMul(Gal, Mil);
                    // Display the results.
                    resultLabel.setText(String.format(result+" is the product"));
                    }
                    catch(NumberFormatException | NegativeDoubleException e) {
                        JOptionPane.showMessageDialog(null,e.getMessage()); //what happens if you dont enter a number
                    } 

             }
          }); 
    }
}

0 个答案:

没有答案