javafx.fxml.LoadException错误!有任何想法吗?

时间:2020-07-18 04:50:48

标签: java javafx

我正在尝试完成JavaFX todoList程序并将其集成到Docker中,但是经过几天的Google搜索和堆栈溢出跟踪,我一直遇到相同的错误。我想知道是否有人知道我的代码有什么问题。

我的项目从登录页面切换到注册页面。即使程序很好地加载了第一个'login.fxml',当程序尝试从'login.fxml'切换到'signUp.fxml'时,我仍然收到以下错误消息:

javafx.fxml.LoadException:

javafx.fxml.LoadException: 
/C:/Users/jkcar/IdeaProjects/toDoList/out/production/toDoList/sample/view/signUp.fxml

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
    at sample.controller.loginController.lambda$initialize$0(loginController.java:54)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879)
    at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
    at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    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)
    at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: 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 com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2591)
    ... 49 more
Caused by: java.lang.NullPointerException
    at sample.database.userDatabase.<init>(userDatabase.java:40)
    at sample.controller.signUpController.initialize(signUpController.java:45)
    ... 60 more

程序仍然加载signUp.fxml页面,但是错误阻止页面实际注册用户。
userDatabase.java

package sample.database;
import javax.sql.DataSource;
import javax.swing.*;
import java.io.*;
import java.util.*;

public class userDatabase{
    //INSTANCE VARIABLES
    private ArrayList<user> allUsers;
    private ArrayList<String[]> rawDataRows= new ArrayList<>();
    private String filePath;

    //CONSTRUCTOR
    public userDatabase(String fileName){
        File file = new File(fileName);
        filePath = fileName;

        try{
            Scanner inputStream = new Scanner(file);
            inputStream.next(); //ignores first line
            while (inputStream.hasNext()){
                String data = inputStream.nextLine(); //gets whole line
                System.out.println(data);
                String[] values = data.split(","); //splits by commas
                rawDataRows.add(values); //adds the line to an arraylist (dataRows)
            }
            inputStream.close();
        } catch (FileNotFoundException e){
            JOptionPane.showMessageDialog(null, "userDatabase Constructor error");
            e.printStackTrace();
        }



        //Creation of each User
        System.out.println(rawDataRows.size()+" rows X "+ rawDataRows.get(1).length + " columns");
        for(int i =1; i<rawDataRows.size();i++){
            user tempUser = new user (rawDataRows.get(i)[0], rawDataRows.get(i)[1], rawDataRows.get(i)[2], rawDataRows.get(i)[3], rawDataRows.get(i)[4], rawDataRows.get(i)[5], rawDataRows.get(i)[6]);
            //add each tweet to the allTweets array
            allUsers.add(tempUser);
        }
    }

    //METHODS
    //Sign Up user
    public void addUser(String userID, String firstName, String lastName, String username, String password, String location, String gender){
        //ADD USER TO userDatabase CLASS
        /*
        user newUser = new user(userID, firstName, lastName, username, password, location, gender);
        allUsers.add(newUser);
         */

        //ADD USER TO user.csv FILE
        try{
            FileWriter fw  = new FileWriter(filePath, true);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter pw = new PrintWriter(bw);

            pw.println(userID+","+firstName+","+lastName+","+username+","+password+","+location+","+gender+"\n");
            pw.flush();
            pw.close();

            JOptionPane.showMessageDialog(null, "User Saved");

        }catch (Exception E){
            JOptionPane.showMessageDialog(null, "User NOT SAVED");
        }
    }

    public String getNumUsers(){
        return allUsers.size()+"";
    }

}

loginController.java

package sample.controller;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javax.print.DocFlavor;
import javax.swing.*;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class loginController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TextField loginUsername;

    @FXML
    private TextField loginPassowrd;

    @FXML
    private Button loginButton;

    @FXML
    private Button loginSignUpButton;

    @FXML
    void initialize() {

        String loginText = loginUsername.getText().trim();
        String loginPwd = loginPassowrd.getText().trim();

        loginSignUpButton.setOnAction(actionEvent -> {
            //Take Users to sign up Screen
            loginSignUpButton.getScene().getWindow().hide(); //hide this window

            //OLD LOADER
            FXMLLoader loader = new FXMLLoader();

            loader.setLocation(getClass().getResource("/sample/view/signUp.fxml")); //error here?

            try {
                loader.load();
            } catch (IOException e) {
                JOptionPane.showMessageDialog(null, "loginController: Old Loader error");
                e.printStackTrace();
            }
            //Works Fine
            Parent root = loader.getRoot();
            Stage stage = new Stage();
            stage.setScene(new Scene(root));
            stage.showAndWait();
        });

        loginButton.setOnAction(event -> {
            if(!loginText.equals("") || !loginPwd.equals("")) {
                loginUser(loginText, loginPwd);
            }else{
                System.out.println("Login Error: Blank Username/Password");
            }
        });
    }

    private void loginUser(String userName, String password){

    }

}

signUp.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="400.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.controller.signUpController">
    <children>
      <AnchorPane prefHeight="50.0" prefWidth="700.0" style="-fx-background-color: #ef5350;">
         <children>
            <Label alignment="CENTER" layoutX="101.0" layoutY="-3.0" prefHeight="17.0" prefWidth="499.0" text="To Do List">
               <font>
                  <Font name="BjorkFont" size="40.0" />
               </font>
            </Label>
         </children>
      </AnchorPane>
      <Label alignment="CENTER" layoutX="330.0" layoutY="78.0" text="Sign Up">
         <font>
            <Font name="Times New Roman" size="15.0" />
         </font>
      </Label>
      <TextField fx:id="signUpFirstName" layoutX="258.0" layoutY="108.0" prefHeight="16.0" prefWidth="93.0" promptText="First Name" />
      <TextField fx:id="signUpUsername" layoutX="258.0" layoutY="145.0" prefHeight="25.0" prefWidth="194.0" promptText="Username" />
      <TextField fx:id="signUpLastName" layoutX="359.0" layoutY="108.0" prefHeight="16.0" prefWidth="93.0" promptText="Last Name" />
      <TextField fx:id="signUpLocation" layoutX="258.0" layoutY="217.0" prefHeight="25.0" prefWidth="194.0" promptText="Location" />
      <CheckBox fx:id="signUpCheckBoxMale" layoutX="303.0" layoutY="249.0" mnemonicParsing="false" selected="true" text="Male" />
      <CheckBox fx:id="signUpCheckBoxFemale" layoutX="360.0" layoutY="249.0" mnemonicParsing="false" text="Female" />
      <TextField fx:id="signUpPassowrd" accessibleRole="PASSWORD_FIELD" layoutX="258.0" layoutY="181.0" prefHeight="25.0" prefWidth="194.0" promptText="Password" />
      <Button fx:id="signUpLoginButton" layoutX="326.0" layoutY="291.0" mnemonicParsing="false" text="Login" />
   </children>
</AnchorPane>

文件路径/项目结构
Image of project structure

我认为我已将问题缩小为控制器类“ loginController.java”的问题,因为这是管理页面切换的类。我附上了问题代码,因此,如果您对修复有任何想法,请告诉我!

感谢您提供的任何帮助或想法!

1 个答案:

答案 0 :(得分:0)

根据我发布的堆栈跟踪,类userDatabase的构造函数抛出NullPointerException。具体来说,该异常在文件userDatabase.java的第40行中引发。没有完全声明实例变量“ private ArrayList rawDataRows = new ArrayList <>()”。

相关问题