由于 ArrayList,主构造函数中的 JavaFX 异常

JavaFX Exception in Main Constructor due to ArrayList

我是 JavaFx 编程的新手,现在在我的初始阶段,我正在用 javaFX 构建地址簿,但问题仍然存在,它在 Main Class 的构造函数中抛出异常。 当我使用 ObserverableList 时,我没有得到任何类型的 Exception,而当我尝试使用数组存储实体对象时,我得到了异常。 谁能给我解决这个问题的方法? 我想做的主要事情是 save ObserverableListA​​rrayList object to a file.

package com.company;
import com.company.Controllers.DatabaseConnectJDBC;      
import com.company.Controllers.MainWindowController;
import com.company.Controllers.secondWindowController;
import com.company.Model.EmployeeEntity;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import java.io.*;
import java.util.ArrayList;

public class Main extends Application {

Stage primaryStage;
@Override
public void start(Stage primaryStage) throws Exception {
    this.primaryStage=primaryStage;
    mainWindow();
}
public void mainWindow()  {
try{
        FXMLLoader loader=new FXMLLoader(this.getClass().getResource("Views/MainWindow.fxml"));
   AnchorPane pane=loader.load();
   Scene scene=new Scene(pane);
        MainWindowController controller=loader.getController();
        controller.setMain(this);
        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.show();


    }catch (Exception e){
        e.printStackTrace();
    }
}

public void secondaryWindow()  {
    try{
        FXMLLoader loader=new FXMLLoader(this.getClass().getResource("Views/secondaryWindow.fxml"));
        AnchorPane pane=loader.load();
        Scene scene=new Scene(pane);
        Stage secondaryStage=new Stage();
        secondWindowController controller=loader.getController();
        controller.setMain(this,secondaryStage);
        secondaryStage.setScene(scene);
        secondaryStage.setResizable(false);
        secondaryStage.show();


    }catch (Exception e){
        e.printStackTrace();
    }
}
private ObservableList<EmployeeEntity> employeeObserveList= FXCollections.observableArrayList();
private ArrayList<EmployeeEntity> arrayList;
public ObservableList<EmployeeEntity> getEmployeeObserveList(){return employeeObserveList;}
public void setEmployeeObserveList(EmployeeEntity person){
    getEmployeeObserveList().add(person);
}

public Main()
{

    arrayList.add(new EmployeeEntity("3","126-B Millat Town","030123456","Abubaker","Usman"));
    arrayList.add(new EmployeeEntity("4","127-B Millat Town","032123456","Asma","Rasool"));
    arrayList.add(new EmployeeEntity("5","128-B Millat Town","035123456","Talha","Farooq"));

    employeeObserveList = FXCollections.observableArrayList(arrayList);
 employeeObserveList.addAll(arrayList);
}

异常


Exception in Application constructor
Exception in thread "main" java.lang.RuntimeException: Unable to construct Application instance: class com.company.Main
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication6(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:819)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater5(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$null9(WinApplication.java:191)
    ... 1 more
Caused by: java.lang.NullPointerException
    at com.company.Main.<init>(Main.java:68)
    ... 13 more

更改此行

private ArrayList<EmployeeEntity> arrayList;

private List<EmployeeEntity> arrayList = new ArrayList<EmployeeEntity>();

您从未初始化 ArrayList。