由于ArrayList

时间:2016-01-13 21:11:54

标签: java arraylist collections javafx observablelist

我是编程JavaFx的新手,现在在我的初始阶段我正在javaFX中构建一个地址簿,但问题是它在Main类的构造函数中抛出异常。 当我使用 ObserverableList 时,我没有得到任何异常,而当我尝试使用数组来存储实体对象时,我得到了异常。 任何人都能帮我解决这个问题吗? 我想做的主要是保存 ObserverableList ArrayList 对象到文件。

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

1 个答案:

答案 0 :(得分:0)

更改此行

private ArrayList<EmployeeEntity> arrayList;

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

您从未初始化ArrayList。