应用程序启动方法java.lang.reflect.InvocationTargetException(JavaFx)和Java中的异常

时间:2018-12-10 17:04:38

标签: java intellij-idea javafx

代码中有问题,无法解决可以帮助我

import javafx.application.Application;
import javafx.event.ActionEvent;``
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;

import java.util.Scanner;

public class Main extends Application {


    public static double Price;
    private static Double Qua;
    private static int Num;
    private static double cq;
    private static String FirstName;
    private static String LastName;

    public static void main(String[] args)
    {
    launch(args);

        CQ n=new CQ();
        System.out.print("First Name: ");
        n.FirstName=FirstName;
        System.out.print("Last Name : ");
        n.LastName=LastName;
        System.out.print("Phone Number: ");
        n.Num=Num;
        System.out.print("Quantity: ");
        n.Qua=Qua;
        System.out.print("Price : ");
        n.Price=Price;
        n.Printinformation();
        n.cq=cq;
    }
    @Override
    public void start(Stage primaryStage) {

        Group root=new Group();
        Scene scene = new Scene(root,600,600,Color.BLANCHEDALMOND);

        Label lab1=new Label("First Name");
        lab1.setLayoutX(300);
        lab1.setLayoutY(50);
        Label lab2=new Label("Last Name");
        lab2.setLayoutX(50);
        lab2.setLayoutY(50);
        Label lab3=new Label("Number Phone");
        lab3.setLayoutX(50);
        lab3.setLayoutY(100);
        Label lab4=new Label("Quntity");
        lab4.setLayoutX(300);
        lab4.setLayoutY(100);
        Label lab5=new Label("Price Press");
        lab5.setLayoutX(50);
        lab5.setLayoutY(150);
        Label lab6=new Label("");
        lab6.setLayoutX(300);
        lab6.setLayoutY(150);

        TextField txtname=new TextField();
        FirstName=String.valueOf(txtname.toString());
        txtname.setLayoutX(300);
        txtname.setLayoutY(70);
        TextField txtname2=new TextField();
        LastName=String.valueOf(txtname2.toString());
        txtname2.setLayoutX(50);
        txtname2.setLayoutY(70);
        TextField txtname3=new TextField();
        Num=Integer.valueOf(txtname3.getText());
        txtname3.setLayoutX(50);
        txtname3.setLayoutY(120);
        TextField txtname4=new TextField();
        Qua=Double.valueOf(txtname4.getText());
        txtname4.setLayoutX(300);
        txtname4.setLayoutY(120);
        TextField txtname5=new TextField();
        Price =Double.valueOf(txtname5.getText());
        txtname5.setLayoutX(50);
        txtname5.setLayoutY(170);
        TextField txtname6=new TextField();
        txtname6.setLayoutX(300);
        txtname6.setLayoutY(170);


        root.getChildren().add(txtname);
        root.getChildren().add(txtname2);
        root.getChildren().add(txtname3);
        root.getChildren().add(txtname4);
        root.getChildren().add(txtname5);
        root.getChildren().add(txtname6);

        root.getChildren().add(lab1);
        root.getChildren().add(lab2);
        root.getChildren().add(lab3);
        root.getChildren().add(lab4);
        root.getChildren().add(lab5);
        root.getChildren().add(lab6);
        Button btn=new Button("Click Her");
        btn.setLayoutX(50);
        btn.setLayoutY(200);
        root.getChildren().add(btn);


        primaryStage.setTitle("Olive Press");
        primaryStage.setScene(scene);
        primaryStage.show();



        btn.setOnAction((ActionEvent e) -> {
            if (txtname3.getText().equals("") || txtname4.getText().equals("") || txtname5.getText().equals(""))
                txtname6.setText("");

            else {
                try {


                    txtname6.setText(String.valueOf(Double.valueOf(cq)));
                }
                catch (Exception ex) {
                    txtname6.setText("error");
                }
            }
        });


}

}

以下是在应用程序启动方法中引发的异常:

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:564)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
    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:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
    at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.NumberFormatException: For input string: ""
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.base/java.lang.Integer.parseInt(Integer.java:662)
    at java.base/java.lang.Integer.valueOf(Integer.java:983)
    at OlivePress.Main.start(Main.java:80)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
    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:175)
    ... 1 more
Exception running application OlivePress.Main

Process finished with exit code 1

1 个答案:

答案 0 :(得分:0)

检查txtname3输入值。您正在传递空字符串“”(来自txtname3.getText()),由于没有为方法参数传递有效的整数,因此它会给NumberFormatException加薪。

Num=Integer.valueOf(txtname3.getText());

希望这会有所帮助!