将ObservableList从一个类传递到另一个类

时间:2016-05-07 18:23:39

标签: java javafx

您好我在尝试将ObservableList从一个类传递到另一个类但似乎收到错误

  

显示java.lang.NullPointerException

java.lang.NullPointerException
	at animal_sanctuary.maintenanceController.populateBreedTable(maintenanceController.java:99)
	at animal_sanctuary.maintenanceController.initialize(maintenanceController.java:44)
	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
	at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
	at animal_sanctuary.Controller.loadScreen(Controller.java:133)
	at animal_sanctuary.Main.start(Main.java:33)
	at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
	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)
	at java.lang.Thread.run(Thread.java:745)

你能告诉我我做错了什么让我发疯了。

这是我的代码

第1课

//===================================================
    //      ***** GET INFO FROM DB *****
    //===================================================
    
    public ObservableList<Breed> getBreed(){

        Statement stmt = null;

        ArrayList<Breed> bre = new ArrayList<Breed>();
        try {
            stmt = conn.createStatement();
            ResultSet rs;
            rs = stmt.executeQuery("SELECT * FROM breeds;");
            while(rs.next()){
                String s = rs.getString("breed");
                System.out.println(s);
                Breed b = new Breed(s);
                bre.add(b);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        ObservableList<Breed> myObservableList = FXCollections.observableArrayList(bre);;

        return myObservableList;
    }

第2课

public void populateBreedTable(){

        ObservableList<Breed> b = myController.getBreed();
        //breedCol.setCellValueFactory(new PropertyValueFactory<>("breed"));
        //itemTable.setItems();

    }

感谢您的时间:)

**更新

  

public class maintenanceController实现Initializable,ControlledScreen {

Controller myController;
Connection conn;

@FXML
RadioButton rbType ,rbBreed, rbLocation;
@FXML
TextField addItemTb;
@FXML
TableView<Breed>  itemTable;
@FXML
TableColumn<Breed, String > breedCol;
@FXML
TableView<Type>  itemTable1;
@FXML
TableColumn<Type, String > TypeCol;


/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
    try {
        conn = myController.getConnection();
        populateBreedTable();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void setScreenParent(Controller screenParent) {
    myController = screenParent;
}

@FXML
private void goToMainScreen(ActionEvent event) {
    myController.setScreen(Main.mainScreen1ID);
}

**

  

public boolean setScreen(final String name){           if(screens.get(name)!= null){//加载屏幕               final DoubleProperty opacity = opacityProperty();

        if (!getChildren().isEmpty()) {    //if there is more than one screen
            Timeline fade = new Timeline(
                    new KeyFrame(Duration.ZERO, new KeyValue(opacity, 1.0)),
                    new KeyFrame(new Duration(1000), new EventHandler<ActionEvent>() {
                        @Override
                        public void handle(ActionEvent t) {
                            getChildren().remove(0);                    //remove the displayed screen
                            getChildren().add(0, screens.get(name));     //add the screen
                            Timeline fadeIn = new Timeline(
                                    new KeyFrame(Duration.ZERO, new KeyValue(opacity, 0.0)),
                                    new KeyFrame(new Duration(800), new KeyValue(opacity, 1.0)));
                            fadeIn.play();
                        }
                    }, new KeyValue(opacity, 0.0)));
            fade.play();

        } else {
            setOpacity(0.0);
            getChildren().add(screens.get(name));       //no one else been displayed, then just show
            Timeline fadeIn = new Timeline(
                    new KeyFrame(Duration.ZERO, new KeyValue(opacity, 0.0)),
                    new KeyFrame(new Duration(2500), new KeyValue(opacity, 1.0)));
            fadeIn.play();
        }
        return true;
    } else {
        System.out.println("screen hasn't been loaded!!! \n");
        return false;
    }
}

1 个答案:

答案 0 :(得分:1)

问题源于执行方法的顺序。作为加载fxml文件的过程的一部分,initialize()会调用FXMLLoader。由于您的initialize()方法会调用populateBreedTable(),因此在调用populateBreedTable()之前会调用FXMLLoader.load()方法。

myController变量仅通过调用setScreenParent()进行初始化,在load()上调用FXMLLoader 之后几乎肯定会发生你调用load然后获得对加载器创建的控制器的引用。因此在populateBreedTable()初始化之前调用myController,并且为空。您可以将通话移至populateBreedTable()setScreenParent(),它应该有效。