0

I have problems adding data to my table via text fields and a button "Add", which I created with fxml. With my code I get a java.lang.reflect.InvocationTargetException error. The only thing that works is adding data directly with the observableList. I try to do the following tutorial, but with fxml: https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/table-view.htm#CJAGAAEE

I am a little confused that when I insert data directly into the observableList, it is inserted into the table, but when I insert data into the observableList via the button, it does not work.

I hope I was able to explain my problem clearly and look forward to any answers.

public class CourseController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TableView<CourseModel> myTable;

    @FXML
    private TableColumn<CourseModel, String> columnName;

    @FXML
    private TableColumn<CourseModel, Integer> columnTeilnehmer;

    @FXML
    private TableColumn<CourseModel, String> columnProfessor;

    @FXML
    private TextField textFieldCourseName;

    @FXML
    private TextField textFieldTeilnehmer;

    @FXML
    private TextField textFieldProfessor;

    @FXML
    private Button btnAdd;

    final ObservableList<CourseModel> CourseModelList =
            FXCollections.observableArrayList(
                    new CourseModel("Jacob", 200, "jacob.smith@example.com"),
                    new CourseModel("Isabella", 100, "isabella.johnson@example.com"));

    @FXML
    void addData(ActionEvent event) {
    CourseModelList.add(new CourseModel(textFieldCourseName.getText(),Integer.parseInt(textFieldCourseName.getText()), columnProfessor.getText()));
    textFieldCourseName.clear();
    textFieldTeilnehmer.clear();
    textFieldProfessor.clear();
    //myTable.getItems().add((CourseModel) CourseModelList);
    }

    @FXML
    void initialize() {
        assert myTable != null : "fx:id=\"myTable\" was not injected: check your FXML file 'Course.fxml'.";
        assert columnName != null : "fx:id=\"columnName\" was not injected: check your FXML file 'Course.fxml'.";
        assert columnTeilnehmer != null : "fx:id=\"columnTeilnehmer\" was not injected: check your FXML file 'Course.fxml'.";
        assert columnProfessor != null : "fx:id=\"columnProfessor\" was not injected: check your FXML file 'Course.fxml'.";
        assert textFieldCourseName != null : "fx:id=\"textFieldCourseName\" was not injected: check your FXML file 'Course.fxml'.";
        assert textFieldTeilnehmer != null : "fx:id=\"textFieldTeilnehmer\" was not injected: check your FXML file 'Course.fxml'.";
        assert textFieldProfessor != null : "fx:id=\"textFieldProfessor\" was not injected: check your FXML file 'Course.fxml'.";
        assert btnAdd != null : "fx:id=\"btnAdd\" was not injected: check your FXML file 'Course.fxml'.";


        columnName.setCellValueFactory(new PropertyValueFactory<>("StudentId"));
        columnTeilnehmer.setCellValueFactory(new PropertyValueFactory<>("FirstName"));
        columnProfessor.setCellValueFactory(new PropertyValueFactory<>("LastName"));
        //add your data to the table here.
        myTable.setItems(CourseModelList);



    }




}
public class CourseModel {

    private SimpleStringProperty columnName;
    private SimpleIntegerProperty columnTeilnehmer;
    private SimpleStringProperty columnProfessor;

    public CourseModel(String columnName, int columnTeilnehmer, String columnProfessor) {
        this.columnName = new SimpleStringProperty(columnName);
        this.columnTeilnehmer = new SimpleIntegerProperty(columnTeilnehmer);
        this.columnProfessor = new SimpleStringProperty(columnProfessor);
    }

    public String getStudentId() {
        return columnName.get();
    }

    public void setStudentId(String columnName) {
        this.columnName = new SimpleStringProperty(columnName);
    }

    public int getFirstName() {
        return columnTeilnehmer.get();
    }

    public void setFirstName(int columnTeilnehmer) {
        this.columnTeilnehmer = new SimpleIntegerProperty(columnTeilnehmer);
    }

    public String getLastName() {
        return columnProfessor.get();
    }

    public void setLastName(String columnProfessor) {
        this.columnProfessor = new SimpleStringProperty(columnProfessor);
    }

}


Error Message:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8413)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 45 more
Caused by: java.lang.NumberFormatException: For input string: "wsedfwef"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at controllers.CourseController.addData(CourseController.java:56)
    ... 55 more
marcy_del
  • 57
  • 5
  • Please post the entire stack trace; an `InvocationTargetException` is meaningless without knowing the cause(s). – Slaw Jul 08 '20 at 13:24
  • Note you're using JavaFX properties incorrectly in `CourseModel`. See _Example 1-1_ in [Using JavaFX Properties and Binding](https://docs.oracle.com/javase/8/javafx/properties-binding-tutorial/binding.htm#JFXBD107) to see an example of how to define a property correctly. – Slaw Jul 08 '20 at 13:28
  • 2
    '`java.lang.NumberFormatException: For input string: "wsedfwef"`' You're trying to convert the text in `textFieldCourseName` to an `int`, so if the user doesn't type an integer value, it fails. – James_D Jul 08 '20 at 13:37
  • @Slaw Ok I edited my question, sorry. Do you mean the setter ? I tried something like this ```this.columnName.set(columnName);``` before but in that case also the input I do directly in the observableList is not showing. – marcy_del Jul 08 '20 at 13:49
  • @Jame_D Oh no, I overlooked it completely. This solves the issue. Sorry, I should have analyze the error message more carefully. Thank you :) – marcy_del Jul 08 '20 at 13:55

0 Answers0