1

我是 JavaFx 编程新手,现在在我的初始阶段,我正在 javaFX 中构建一个地址簿,但问题是它在 Main Class 的构造函数中引发异常。当我使用ObserverableList时,我没有得到任何类型的异常,而当我尝试使用数组来存储实体对象时,我得到了异常。谁能给我一个解决这个问题的方法?我要做的主要事情是 ObserverableListArrayList对象保存到文件中。

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$launchApplication$156(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$launchApplication1$162(LauncherImpl.java:819)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(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$null$149(WinApplication.java:191)
    ... 1 more
Caused by: java.lang.NullPointerException
    at com.company.Main.<init>(Main.java:68)
    ... 13 more

4

1 回答 1

0

更改此行

private ArrayList<EmployeeEntity> arrayList;

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

您从未初始化过 ArrayList。

于 2016-01-13T21:17:27.333 回答