I'm using JavaFX 8. Right now I keep getting a NullPointerException for my tables. I have initalized the data and it appears when I click on the button to bring it up. However, when I try to search or delete something from the table it keeps giving me the exception. I've sifted through my code and double checked, but I cannot figure out why. It is only happening in my Modify Products.
Here are my code snippets.
MainApp.java
private final ObservableList<Part> PartsData = FXCollections.observableArrayList();
private final ObservableList<Product> ProductsData = FXCollections.observableArrayList();
public MainApp() {
PartsData.add(new Inhouse("Part 1", 1,1.99,10, "", 9874));
PartsData.add(new Inhouse("Part 2", 2,1.99,10, "", 1234));
PartsData.add(new Outsourced("Part 3", 3,1.99,10));
PartsData.add(new Outsourced("Part 4", 4,1.99,10));
ProductsData.add(new Product(1,"Product 1",3,2.99, PartsData.get(0)));
ProductsData.add(new Product(2,"Product 2",3,2.99, PartsData.get(1)));
ProductsData.add(new Product(3,"Product 3",3,2.99, PartsData.get(2)));
ProductsData.add(new Product(4,"Product 4",3,2.99, PartsData.get(3)));
}
public ObservableList<Part> getPartsData(){
return PartsData;
}
public ObservableList<Product> getProductsData(){
return ProductsData;
}
public boolean showModifyProducts(Product products) {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/modProduct.fxml"));
AnchorPane page = (AnchorPane) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("Modify Product");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
ModProductController controller2 = loader.getController();
controller2.setPartsTable(PartsData);
controller2.setDialogStage(dialogStage);
controller2.setProduct(products);
dialogStage.showAndWait();
return controller2.isOkClicked();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
ModProductController.java
@FXML
private void initialize(){
partIDColumn.setCellValueFactory(cellData -> cellData.getValue().partIDProperty().asObject());
partNameColumn.setCellValueFactory(cellData -> cellData.getValue().partNameProperty());
partInvColumn.setCellValueFactory(cellData -> cellData.getValue().instockProperty().asObject());
partCostColumn.setCellValueFactory(cellData -> cellData.getValue().priceProperty().asObject());
ApartIDColumn.setCellValueFactory(cellData -> cellData.getValue().partIDProperty().asObject());
ApartNameColumn.setCellValueFactory(cellData -> cellData.getValue().partNameProperty());
ApartInvColumn.setCellValueFactory(cellData -> cellData.getValue().instockProperty().asObject());
ApartCostColumn.setCellValueFactory(cellData -> cellData.getValue().priceProperty().asObject());
grpProduct = new Product();
}
public void setPartsTable(List<Part> parts) {
partsTable.getItems().setAll(parts);
}
public void setProduct(Product product) {
this.grpProduct = product;
modProductIdTxt.setText(Integer.toString(product.getProductID()));
modProductName.setText(product.getProductName());
modProductInv.setText(Integer.toString(product.getProductInv()));
modProductPrice.setText(Double.toString(product.getProductPrice()));
modProductMin.setText(Integer.toString(product.getProductMin()));
modProductMax.setText(Integer.toString(product.getProductMax()));
ApartsTable.setItems(product.getlist());
}
@FXML
private void removePart() {
Part selectedPart = ApartsTable.getSelectionModel().getSelectedItem();
if (selectedPart != null) {
mainApp.getPartsData().add(selectedPart); //Line 144
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Confirm Delete");
alert.setContentText("Do you want to delete " + selectedPart.getPartName() + "?");
alert.showAndWait()
.filter(response -> response == ButtonType.OK)
.ifPresent(response -> grpProduct.getlist().remove(selectedPart));
ApartsTable.setItems(grpProduct.getlist());
}
}
@FXML
public void searchApart() {
String searchAparts = modApartsTableSearch.getText();
FilteredList<Part> searchText = searchApart(searchAparts); //Line 287
SortedList<Part> sortedData = new SortedList<>(searchText);
sortedData.comparatorProperty().bind(ApartsTable.comparatorProperty());
ApartsTable.setItems(sortedData);
}
public FilteredList<Part> searchApart(String s) {
return mainApp.getPartsData().filtered(p -> p.getPartName().toLowerCase().contains(s.toLowerCase())); //Line 294
}
This pops up in my Output.
Caused by: java.lang.NullPointerException
at inventory.view.ModProductController.removePart(ModProductController.java:144)
at inventory.view.ModProductController.searchApart(ModProductController.java:294)
at inventory.view.ModProductController.searchApart(ModProductController.java:287)
I hope I got all the relevant code. Any help will be appreciated! Thanks!