应用程序启动方法/位置中的 JavaFX 异常需要无修复

JavaFX Exception in Application start method / Location is required no fix

我试图在一组 courses/tutorials 之后在 JavaFX 中创建一个图书馆管理系统,我遇到了这些问题:java.lang.RuntimeException:应用程序启动方法异常 & java.lang.NullPointerException:需要位置。

我已经检查了很多关于这 2 个特定问题的主题,但似乎没有任何帮助。 我正在使用 SceneBuilder 9.0.1 / Java 8.0 / NetBeans 8.2.

请记住:我是 java/javaFX 的新手,我正在尝试在学习这些主题的同时完成作业。将不胜感激。

代码如下:

package bibliotecajfx.ui.adaugacarte;

import bibliotecajfx.*;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author Munteanu Mihail
 */
public class BibliotecaJFX extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("/FXMLDocument.fxml"));

        Scene scene = new Scene(root);

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

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

}

还有 FXMLController 的副本:

package bibliotecajfx.ui.adaugacarte;

import bibliotecajfx.*;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

/**
 *
 * @author Munteanu Mihail
 */
public class FXMLDocumentController implements Initializable {

    private Label label;
    @FXML
    private JFXTextField title;
    @FXML
    private JFXTextField id;
    @FXML
    private JFXTextField editura;
    @FXML
    private JFXTextField autor;
    @FXML
    private JFXButton saveButton;
    @FXML
    private JFXButton cancelButton;

    DatabaseHandler databaseHandler;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        databaseHandler = new DatabaseHandler();
    }    

    @FXML
    private void addBook(ActionEvent event) {
    }

    @FXML
    private void cancel(ActionEvent event) {
    }

}

数据库代码:

package bibliotecajfx.database;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//import jdk.nashorn.internal.ir.Statement;

/**
 *
 * @author Munteanu Mihail
 */
public class DatabaseHandler {
    private static DatabaseHandler handler;

    private static final String DB_URL = "jdbc:derby:database;create:true";
    private static Connection conn = null;
    private static Statement stat = null;

    public DatabaseHandler(){
        createConnection();
        setupBookTable();
    }

    void createConnection(){
        try{
            Class.forName("org.apache.derby.jdbc.EnableDriver").newInstance();
            conn = DriverManager.getConnection(DB_URL);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    void setupBookTable(){
        String TABLE_NAME= "CARTE";
        try{
            stat = conn.createStatement();
            DatabaseMetaData dbm = conn.getMetaData();
            ResultSet tables = dbm.getTables(null, null, TABLE_NAME.toUpperCase(), null);
            if(tables.next()){
                System.out.println("Tabela"+TABLE_NAME+"exista deja!");
            }else{
                stat.execute("TABELA CREATA "+TABLE_NAME+"("
                +"      id varchar(200) primary key, \n"
                +"      titlu varchar(200), \n"
                +"      autor varchar(200),\n"
                +"      editura varchar(100),\n"
               // +"      intcode varchar(100),\n"
                +"      isAvail boolean defaul true"
                +" )");
            }
        }catch(SQLException e){
            System.err.println(e.getMessage()+" === init Database");
        }finally{

        }
    }
}

和日志:

ant -f "C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX" -Djavafx.main.class=bibliotecajfx.ui.adaugacarte.BibliotecaJFX jfxsa-run
init:
deps-clean:
Updating property file: C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\build\built-clean.properties
Deleting directory C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\build
clean:
init:
deps-jar:
Created dir: C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\build
Updating property file: C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\build\built-jar.properties
Created dir: C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\build\classes
Created dir: C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\build\empty
Created dir: C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\build\generated-sources\ap-source-output
Compiling 4 source files to C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\build\classes
warning: C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\jfoenix-9.0.0.jar(com/jfoenix/controls/JFXButton.class): major version 53 is newer than 52, the highest major version supported by this compiler.
  It is recommended that the compiler be upgraded.
warning: C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\jfoenix-9.0.0.jar(com/jfoenix/controls/JFXTextField.class): major version 53 is newer than 52, the highest major version supported by this compiler.
  It is recommended that the compiler be upgraded.
2 warnings
Copying 2 files to C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\build\classes
compile:
Created dir: C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\dist
Copying 3 files to C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\dist\lib
Detected JavaFX Ant API version 1.3
Going to create default keystore in C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\build\nb-jfx.jks
Generating Key for nb-jfx

Warning:
The JKS keystore uses a proprietary format. It is recommended to migrate to PKCS12 which is an industry standard format using "keytool -importkeystore -srckeystore C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\build\nb-jfx.jks -destkeystore C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\build\nb-jfx.jks -deststoretype pkcs12".
Launching <fx:jar> task from C:\Program Files\Java\jdk1.8.0_152\jre\..\lib\ant-javafx.jar
Warning: From JDK7u25 the Codebase manifest attribute should be used to restrict JAR repurposing.
         Please set manifest.custom.codebase property to override the current default non-secure value '*'.
Signing JAR: C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\dist\bibliotecaJFX.jar to C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\dist\bibliotecaJFX.jar as nb-jfx
jar signed.

Warning: 
The signer certificate will expire within six months.
No -tsa or -tsacert is provided and this jar is not timestamped. Without a timestamp, users may not be able to validate this jar after the signer certificate's expiration date (2018-04-12) or after any future revocation date.
Enter Passphrase for keystore: Enter key password for nb-jfx: 
Signing JAR: C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\dist\lib\derby.jar to C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\dist\lib\derby.jar as nb-jfx
jar signed.

Warning: 
The signer certificate will expire within six months.
No -tsa or -tsacert is provided and this jar is not timestamped. Without a timestamp, users may not be able to validate this jar after the signer certificate's expiration date (2018-04-12) or after any future revocation date.
Enter Passphrase for keystore: Enter key password for nb-jfx: 
Signing JAR: C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\dist\lib\derbyclient.jar to C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\dist\lib\derbyclient.jar as nb-jfx
jar signed.

Warning: 
The signer certificate will expire within six months.
No -tsa or -tsacert is provided and this jar is not timestamped. Without a timestamp, users may not be able to validate this jar after the signer certificate's expiration date (2018-04-12) or after any future revocation date.
Enter Passphrase for keystore: Enter key password for nb-jfx: 
Signing JAR: C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\dist\lib\jfoenix-9.0.0.jar to C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\dist\lib\jfoenix-9.0.0.jar as nb-jfx
jar signed.

Warning: 
The signer certificate will expire within six months.
No -tsa or -tsacert is provided and this jar is not timestamped. Without a timestamp, users may not be able to validate this jar after the signer certificate's expiration date (2018-04-12) or after any future revocation date.
Enter Passphrase for keystore: Enter key password for nb-jfx: 
Launching <fx:deploy> task from C:\Program Files\Java\jdk1.8.0_152\jre\..\lib\ant-javafx.jar
No base JDK. Package will use system JRE.
No base JDK. Package will use system JRE.
jfx-deployment-script:
jfx-deployment:
jar:
Copying 15 files to C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\dist\run1897499086
jfx-project-run:
Executing C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\dist\run1897499086\bibliotecaJFX.jar using platform C:\Program Files\Java\jdk1.8.0_152\jre/bin/java
Exception in Application start method
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:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    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:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication4(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.UnsupportedClassVersionError: com/jfoenix/controls/JFXButton has been compiled by a more recent version of the Java Runtime (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access0(URLClassLoader.java:73)
    at java.net.URLClassLoader.run(URLClassLoader.java:368)
    at java.net.URLClassLoader.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:2916)
    at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2905)
    at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2846)
    at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2692)
    at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2661)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at bibliotecajfx.ui.adaugacarte.BibliotecaJFX.start(BibliotecaJFX.java:23)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication11(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait4(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null2(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater3(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null7(WinApplication.java:177)
    ... 1 more
Exception running application bibliotecajfx.ui.adaugacarte.BibliotecaJFX
Java Result: 1
Deleting directory C:\Users\Munteanu Mihail\Desktop\bibliotecaJFX\dist\run1897499086
jfxsa-run:
BUILD SUCCESSFUL (total time: 20 seconds)

项目布局:

首先我要说你的 FXML 文件的路径是错误的。根据你的项目布局,应该是

Parent root = FXMLLoader.load(getClass().getResource("/bibliotecajfx/ui/adaugacarte/FXMLDocument.fxml"));

因为那是你的 java 包树。开头的/表示路径是从jar文件根目录开始的绝对路径。

此外,您使用的 JFoenix 库已使用较新版本的 Java 编译,因此您应该更新您的库。或者至少确保 JFoenix 的版本和你的 Java 版本相同。

干杯