如何解决“应用程序启动方法中的异常”问题

问题描述 投票:-2回答:1

我正在使用netbeans和场景构建器创建一个小型登录应用程序!首先,一切正常,应用程序正确运行。

然后,当我添加按钮的动作处理程序时,当我运行该应用程序时,会显示GUI,但是按钮不起作用。

然后突然,该应用程序不再起作用。

我试图在代码中找到错误,我发现它在这一行:Parent root = FXMLLoader.load(getClass().getResource("/FXMLDocument.fxml"));

我已经在许多线程中搜索了解决方案,但是它们都没有解决问题!

这是我在项目中的文件:

FXML文件:

  <?xml version="1.0" encoding="UTF-8"?>

<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXTextField?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.text.Font?>

<fx:root id="AnchorPane" prefHeight="500.0" prefWidth="968.0" type="AnchorPane" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication2.FXMLDocumentController">
 <children>
    <StackPane layoutX="527.0" layoutY="-2.0" prefHeight="500.0" prefWidth="461.0">
       <children>
          <AnchorPane fx:id="pn_Home" prefHeight="500.0" prefWidth="461.0" style="-fx-background-color: #fff;">
             <children>
                <Label layoutX="202.0" layoutY="88.0" text="Hello,">
                   <font>
                      <Font size="28.0" />
                   </font>
                </Label>
                <Label layoutX="92.0" layoutY="171.0" text="This software is developped by Seifenn">
                   <font>
                      <Font size="17.0" />
                   </font>
                </Label>
                <Label layoutX="158.0" layoutY="197.0" text="as a personal project.">
                   <font>
                      <Font size="17.0" />
                   </font>
                </Label>
                <Label layoutX="158.0" layoutY="274.0" text="Feel free to contact me" />
                <MaterialDesignIconView glyphName="VPN" layoutX="212.0" layoutY="443.0" size="60" />
                <JFXButton contentDisplay="GRAPHIC_ONLY" layoutX="130.0" layoutY="305.0" prefHeight="40.0" prefWidth="40.0">
                   <graphic>
                      <MaterialDesignIconView fill="#211f1f" glyphName="GITHUB_BOX" size="40" />
                   </graphic>
                </JFXButton>
                <JFXButton contentDisplay="GRAPHIC_ONLY" layoutX="185.0" layoutY="305.0" prefHeight="40.0" prefWidth="60.0">
                   <graphic>
                      <MaterialDesignIconView fill="#0e76a8" glyphName="LINKEDIN_BOX" size="40" wrappingWidth="40.0" />
                   </graphic>
                </JFXButton>
                <JFXButton contentDisplay="GRAPHIC_ONLY" layoutX="242.0" layoutY="306.0" prefHeight="40.0" prefWidth="40.0">
                   <graphic>
                      <MaterialDesignIconView fill="#3b5998" glyphName="FACEBOOK_BOX" size="40" />
                   </graphic>
                </JFXButton>
                <JFXButton contentDisplay="GRAPHIC_ONLY" layoutX="302.0" layoutY="301.0" prefHeight="40.0" prefWidth="40.0">
                   <graphic>
                      <MaterialDesignIconView fill="#b23121" glyphName="GMAIL" size="47" text="" wrappingWidth="45.99999916553497" />
                   </graphic>
                </JFXButton>
                <JFXButton contentDisplay="TEXT_ONLY" layoutX="131.0" layoutY="359.0" prefHeight="38.0" prefWidth="214.0" text="Follow me on Youtube !" textFill="#c4302b">
                   <font>
                      <Font size="17.0" />
                   </font>
                </JFXButton>
             </children>
          </AnchorPane>
          <AnchorPane fx:id="pn_SignUp" prefHeight="500.0" prefWidth="461.0" style="-fx-background-color: #fffFFF;">
             <children>
                <Label layoutX="172.0" layoutY="71.0" prefHeight="50.0" prefWidth="118.0" text="Sign Up" textFill="#1a33d5">
                   <font>
                      <Font size="33.0" />
                   </font>
                </Label>
                <JFXTextField layoutX="66.0" layoutY="146.0" opacity="0.5" prefHeight="50.0" prefWidth="330.0" promptText="Username">
                   <font>
                      <Font size="17.0" />
                   </font>
                </JFXTextField>
                <JFXTextField layoutX="66.0" layoutY="217.0" opacity="0.5" prefHeight="50.0" prefWidth="330.0" promptText="Email">
                   <font>
                      <Font size="17.0" />
                   </font>
                </JFXTextField>
                <JFXPasswordField layoutX="66.0" layoutY="288.0" opacity="0.5" prefHeight="50.0" prefWidth="330.0" promptText="Password">
                   <font>
                      <Font size="17.0" />
                   </font>
                </JFXPasswordField>
                <JFXButton contentDisplay="GRAPHIC_ONLY" layoutX="195.0" layoutY="404.0">
                   <graphic>
                      <FontAwesomeIconView fill="#1a33d5" glyphName="CHEVRON_CIRCLE_RIGHT" size="60" wrappingWidth="51.42856979370117" />
                   </graphic>
                </JFXButton>
             </children>
          </AnchorPane>
          <AnchorPane fx:id="pn_SignIn" prefHeight="500.0" prefWidth="461.0" style="-fx-background-color: #ffffff;">
             <children>
                <Label layoutX="176.0" layoutY="71.0" prefHeight="50.0" prefWidth="118.0" text="Sign In" textFill="#1a33d5">
                   <font>
                      <Font size="33.0" />
                   </font>
                </Label>
                <JFXTextField layoutX="66.0" layoutY="173.0" opacity="0.5" prefHeight="50.0" prefWidth="330.0" promptText="Username">
                   <font>
                      <Font size="17.0" />
                   </font>
                </JFXTextField>
                <JFXPasswordField layoutX="66.0" layoutY="261.0" opacity="0.5" prefHeight="50.0" prefWidth="330.0" promptText="Password">
                   <font>
                      <Font size="17.0" />
                   </font>
                </JFXPasswordField>
                <JFXButton contentDisplay="GRAPHIC_ONLY" layoutX="195.0" layoutY="358.0" prefWidth="27.0">
                   <graphic>
                      <FontAwesomeIconView fill="#1a33d5" glyphName="CHEVRON_CIRCLE_RIGHT" size="60" wrappingWidth="51.42856979370117" />
                   </graphic>
                </JFXButton>
             </children>
          </AnchorPane>
       </children>
    </StackPane>
    <AnchorPane layoutX="137.0" prefHeight="501.0" prefWidth="390.0" style="-fx-background-color: #1A33D5;">
       <children>
          <Label layoutX="80.0" layoutY="271.0" text="Welcome to Seifenn" textFill="WHITE">
             <font>
                <Font size="26.0" />
             </font>
          </Label>
          <MaterialDesignIconView fill="WHITE" glyphName="VPN" layoutX="145.0" layoutY="258.0" rotate="-180.0" size="100" />
       </children>
    </AnchorPane>
    <AnchorPane prefHeight="501.0" prefWidth="137.0" style="-fx-background-color: #ffffff;">
       <children>
          <JFXButton fx:id="btn_Home" layoutX="1.0" layoutY="151.0" onAction="#handleButtonAction" prefHeight="50.0" prefWidth="135.0" ripplerFill="#282828" text="Home" textFill="#1a33d5">
             <font>
                <Font size="17.0" />
             </font>
          </JFXButton>
          <JFXButton fx:id="btn_SignIn" layoutX="1.0" layoutY="201.0" onAction="#handleButtonAction" prefHeight="50.0" prefWidth="135.0" ripplerFill="#282828" text="Sign In">
             <font>
                <Font size="17.0" />
             </font>
          </JFXButton>
          <JFXButton fx:id="btn_SignUp" layoutX="1.0" layoutY="251.0" onAction="#handleButtonAction" prefHeight="50.0" prefWidth="135.0" ripplerFill="#282828" text="Sign Up">
             <font>
                <Font size="17.0" />
             </font>
          </JFXButton>
          <JFXButton fx:id="btn_Exit" layoutX="1.0" layoutY="301.0" onAction="#handleButtonAction" prefHeight="50.0" prefWidth="135.0" ripplerFill="#282828" text="Exit">
             <font>
                <Font size="17.0" />
             </font>
          </JFXButton>
       </children>
    </AnchorPane>
 </children>
</fx:root>

控制器文件:

  package javafxapplication2;

  import com.jfoenix.controls.JFXButton;
  import java.net.URL;
  import java.util.ResourceBundle;
  import javafx.event.ActionEvent;
  import javafx.fxml.FXML;
  import javafx.fxml.Initializable;
  import javafx.scene.layout.AnchorPane;

    public class FXMLDocumentController implements Initializable {

    @FXML
    //private Label label;
    private JFXButton btn_Home;
    private JFXButton btn_SignIn;
    private JFXButton btn_SignUp;
    private JFXButton btn_Exit;

    private AnchorPane pn_Home;
    private AnchorPane pn_SignIn;
    private AnchorPane pn_SignUp;



    @FXML
    private void handleButtonAction(ActionEvent event) {
       if (event.getSource() == btn_Home) {
           System.out.println("I am working !!!");
           pn_Home.toFront();

       } 
       else if (event.getSource() == btn_SignIn) {
           System.out.println("I am working !!!");
           pn_SignIn.toFront();

       }
       else if (event.getSource() == btn_SignUp) {
           System.out.println("I am working !!!");
           pn_SignUp.toFront();

       }
       else if (event.getSource() == btn_Exit) {
           System.out.println("I am working !!!");
           // Exit application

       }
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

Java应用程序文件:

package javafxapplication2;


import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;



public class JavaFXApplication2 extends Application {
    private double x0ffset = 0;
    private double y0ffset = 0;
    @Override
    public void start(Stage stage) throws Exception {
        System.out.println("I am here !!");
        System.out.println("I am here !!");
        System.out.println("I am here !!");
        Parent root = FXMLLoader.load(getClass().getResource("/FXMLDocument.fxml"));


        stage.initStyle(StageStyle.TRANSPARENT);

        root.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override public void handle(MouseEvent event) {
                x0ffset = event.getSceneX();
                y0ffset = event.getSceneY();
            }
        });
        root.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override public void handle(MouseEvent event) {
                stage.setX(event.getScreenX() - x0ffset);
                stage.setY(event.getScreenY() - y0ffset);
            }
        });


        Scene scene = new Scene(root);


        scene.setFill(Color.TRANSPARENT);


        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

这是我跑步后的输出:

jfx-project-run:
     [echo] Executing C:\Users\seife\Documents\NetBeansProjects\JavaFXApplication2\dist\run908192858\JavaFXApplication2.jar using platform C:\Program Files\Java\jdk1.8.0_231/bin/java
     [java] I am here !!Exception in Application start method
     [java] I am here !!
     [java] I am here !!
     [java] java.lang.reflect.InvocationTargetException
     [java] 
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
     [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
     [java]     at java.lang.reflect.Method.invoke(Method.java:498)
     [java]     at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
     [java]     at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
     [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
     [java]     at java.lang.reflect.Method.invoke(Method.java:498)
     [java]     at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
     [java] Caused by: java.lang.RuntimeException: Exception in Application start method
     [java]     at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
     [java]     at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
     [java]     at java.lang.Thread.run(Thread.java:748)
     [java] Caused by: java.lang.NullPointerException: Location is required.
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
     [java]     at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
     [java]     at javafxapplication2.JavaFXApplication2.start(JavaFXApplication2.java:32)
     [java]     at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
     [java]     at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
     [java]     at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
     [java]     at java.security.AccessController.doPrivileged(Native Method)
     [java]     at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
     [java]     at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
     [java]     at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
     [java]     at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
     [java]     ... 1 more
     [java] Exception running application javafxapplication2.JavaFXApplication2

-remove-temp-run-dir:
   [delete] Deleting directory C:\Users\seife\Documents\NetBeansProjects\JavaFXApplication2\dist\run908192858

jfxsa-run-no-another-jvm:

BUILD SUCCESSFUL
Total time: 3 seconds
     [java] Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)

非常感谢您对我的帮助!

文件结构。

File structure containing java and fxml files

在主文件中使用System.out.println( JavaFXApplication2.class.getResource("/javafxapplication2/FXMLDocument.fxml") );而不是System.out.println("I am here")之后,我得到了以下输出:

jfx-project-run:
     [echo] Executing C:\Users\seife\Documents\NetBeansProjects\JavaFXApplication2\dist\run37482628\JavaFXApplication2.jar using platform C:\Program Files\Java\jdk1.8.0_231/bin/java
     [java] jar:file:/C:/Users/seife/Documents/NetBeansProjects/JavaFXApplication2/dist/run37482628/JavaFXApplication2.jar!/javafxapplication2/FXMLDocument.fxml
     [java] Exception in Application start method
     [java] java.lang.reflect.InvocationTargetException
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
     [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
     [java]     at java.lang.reflect.Method.invoke(Method.java:498)
     [java]     at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
     [java]     at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
     [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
     [java]     at java.lang.reflect.Method.invoke(Method.java:498)
     [java]     at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
     [java] Caused by: java.lang.RuntimeException: Exception in Application start method
     [java]     at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
     [java]     at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
     [java]     at java.lang.Thread.run(Thread.java:748)
     [java] Caused by: javafx.fxml.LoadException: Root hasn't been set. Use method setRoot() before load.
     [java] file:/C:/Users/seife/Documents/NetBeansProjects/JavaFXApplication2/dist/run37482628/JavaFXApplication2.jar!/javafxapplication2/FXMLDocument.fxml:13
     [java] 
     [java]     at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
     [java]     at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
     [java]     at javafx.fxml.FXMLLoader$RootElement.constructValue(FXMLLoader.java:1326)     [java] Java Result: 1

     [java]     at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:746)
     [java]     at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
     [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
     [java]     at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
     [java]     at javafxapplication2.JavaFXApplication2.start(JavaFXApplication2.java:30)
     [java]     at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
     [java]     at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
     [java]     at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
     [java]     at java.security.AccessController.doPrivileged(Native Method)
     [java]     at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
     [java]     at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
     [java]     at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
     [java]     at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
     [java]     ... 1 more
     [java] Exception running application javafxapplication2.JavaFXApplication2

-remove-temp-run-dir:
   [delete] Deleting directory C:\Users\seife\Documents\NetBeansProjects\JavaFXApplication2\dist\run37482628

jfxsa-run-no-another-jvm:

BUILD SUCCESSFUL
Total time: 9 seconds
java javafx netbeans builder scene
1个回答
0
投票
enter image description here

这对于遇到相同错误的其他人可能很有用:*我的第一个错误是由错误的根Caused by: javafx.fxml.LoadException: Root hasn't been set. Use method setRoot() before load.(在netbeans输出中)引起的,然后更正了FXML中的根加载程序将修复问题。在此代码Parent root = FXMLLoader.load(getClass().getResource("/FXMLDocument.fxml"));中,根是错误的,与我的文件结构不匹配。

* My second error, ```Caused by: java.lang.NullPointerException: Location is required``` (in netbeans output) was caused by this chunk of code in the controller file: ``` @FXML //private Label label; private JFXButton btn_Home; private JFXButton btn_SignIn; private JFXButton btn_SignUp; private JFXButton btn_Exit; private AnchorPane pn_Home; private AnchorPane pn_SignIn; private AnchorPane pn_SignUp; ``` and it is fixed by adding @FXML before every variable declaration!

© www.soinside.com 2019 - 2024. All rights reserved.