0

I am having trouble with Null pointers when trying to initiate Table population. I have my object from class called Komponenta which data I put in ObservableList and then try to show it in a TableView. I've tried this time to work with multiple fxml screens and for some reason it doesn't work with more than 1 .fxml. test is a method that I call when a button is pressed on main.fxml so it loads a new one.

MainController.java

public ObservableList<Komponenta> komponente = FXCollections.observableArrayList(new Komponenta("Neki Tip", "Neki Proizvodac","Neki opis", 5));
@FXML
private TableView<Komponenta> Tablica;
@FXML
private TableColumn<Komponenta,String> Tip;
@FXML
private TableColumn<Komponenta,String> Proizvodac;
@FXML
private TableColumn<Komponenta,String> Karakteristika;
@FXML
private TableColumn<Komponenta,Integer> Komada;
@FXML
private void test(ActionEvent event) throws IOException{
    Node node=(Node) event.getSource();
    Stage stage = (Stage) node.getScene().getWindow();
    Parent root = (Pane)FXMLLoader.load(getClass().getResource("ElKomponenta.fxml"));
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();       
}
@FXML
private void initialize() throws Exception  {
    System.out.println(komponente.get(0).getKomada());
    Tip.setCellValueFactory(new PropertyValueFactory<Komponenta, String>("tip"));
    Proizvodac.setCellValueFactory(new PropertyValueFactory<Komponenta, String>("proizvodac"));
    Karakteristika.setCellValueFactory(new PropertyValueFactory<Komponenta, String>("karakteristika"));
    Komada.setCellValueFactory(new PropertyValueFactory<Komponenta, Integer>("komada"));
    Tablica.setItems(komponente);
}

ElKomponenta.fxml

<TableView fx:id="Tablica" prefHeight="603.0" prefWidth="889.0">
  <columns>
    <TableColumn fx:id="Tip" prefWidth="177.0" text="Tip" />
    <TableColumn fx:id="Proizvodac" minWidth="0.0" prefWidth="207.0" text="Proizvođač" />
      <TableColumn fx:id="Karakteristika" prefWidth="326.0" text="Karakteristike" />
      <TableColumn fx:id="Komada" minWidth="0.0" prefWidth="178.0" text="Komada" />
  </columns>
</TableView>

Error:

Caused by: java.lang.NullPointerException
at application.MainController.initialize(MainController.java:62)
... 27 more

Which is the line that first starts to setCellValueFactory.

Also if needed, Komponenta.java

public class Komponenta {

    private String proizvodac;
    private String tip;
    private String karakteristika;
    private Integer komada;

    public Komponenta(String a, String b, String c, Integer d){
        this.tip = a;
        this.proizvodac = b;
        this.karakteristika = c;
        this.komada = d;
    }

    public String getProizvodac() {
        return proizvodac;
    }

    public void setProizvodac(String proizvodac) {
        this.proizvodac = proizvodac;
    }

    public String getTip() {
        return tip;
    }

    public void setTip(String tip) {
        this.tip = tip;
    }

    public String getKarakteristika() {
        return karakteristika;
    }

    public void setKarakteristika(String karakteristika) {
        this.karakteristika = karakteristika;
    }

    public Integer getKomada() {
        return komada;
    }

    public void setKomada(Integer komada) {
        this.komada = komada;
    }

}

1 Answer 1

1

main.fxml doesn't seem to contain a TableColumn with fx:id Tip, but seems to use the same controller class. Since initialize is called at the end of every invocation of a FXMLLoader.load method that uses the controller instance, loading of main.fxml results in the NullPointerException.

Use different controller classes for both fxmls instead: The one for main.fxml should contain the button action handler and the one for ElKomponenta.fxml should contain the TableView related stuff.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.