Hi there I am trying to pass an ObservableList from one class to another but seem to be getting an error
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
//=================================================== // ***** 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
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 {
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();
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. Since your initialize()
method invokes populateBreedTable()
, the populateBreedTable()
method is invoked before the call to FXMLLoader.load()
completes.
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). Thus populateBreedTable()
is called before myController
is initialized, and is null. You can move the call to populateBreedTable()
to setScreenParent()
and it should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.