简体   繁体   English

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

[英]Passing ObservableList from one class to another

Hi there I am trying to pass an ObservableList from one class to another but seem to be getting an error 嗨,我正在尝试将ObservableList从一个类传递到另一个类,但似乎收到错误

java.lang.NullPointerException 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) 

Can you tell me what i am doing wrong its is driving me crazy . 你能告诉我我在做错什么,这使我发疯。

Here is my Code 这是我的代码

Class 1 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; } 

Class 2 2级

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

Thanks you for your time :) 谢谢您的时间:)

**UPDATE **更新

public class maintenanceController implements Initializable, ControlledScreen { 公共类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) { //screen loaded final DoubleProperty opacity = opacityProperty(); public boolean setScreen(final String name){if(screens.get(name)!= null){//屏幕加载的最终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;
    }
}

The problem arises from the order in which the methods are executed. 问题是由执行方法的顺序引起的。 initialize() is called by the FXMLLoader as part of the process of loading the fxml file. FXMLLoader在加载fxml文件的过程中调用initialize() Since your initialize() method invokes populateBreedTable() , the populateBreedTable() method is invoked before the call to FXMLLoader.load() completes. 由于您的initialize()方法调用populateBreedTable() ,因此在对FXMLLoader.load()的调用完成之前,将调用populateBreedTable()方法。

The myController variable is only initialized by a call to setScreenParent() which almost certainly happens after you call load() on the FXMLLoader (typically you call load and then get a reference to the controller the loader created). myController变量仅通过调用setScreenParent()来初始化,这几乎可以肯定是在FXMLLoader上调用load() 之后发生的(通常是先调用load然后获取对创建的加载器的控制器的引用)。 Thus populateBreedTable() is called before myController is initialized, and is null. 因此,在初始化myController之前将调用populateBreedTable() ,该参数为null。 You can move the call to populateBreedTable() to setScreenParent() and it should work. 您可以将对populateBreedTable()的调用移到setScreenParent() ,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM