0

I'm trying to put data into tableview but i keep getting javafx.fxml.LoadException and i dont know how to solve it, I have seen other posts but they don't seem to work for me ,the error came from table.setItems(data); if I remove table.setItems(data); the error disappear but the table will be populated and thank you for your help.

my APPLICATION

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class App extends Application {

  @Override
  public void start(Stage primaryStage) {
    try {
      Parent parent = FXMLLoader.load(getClass().getResource("/todo.fxml"));
      Scene scene = new Scene(parent);
      scene
        .getStylesheets()
        .add(getClass().getResource("/style.css").toExternalForm());
      primaryStage.setScene(scene);
      primaryStage.setTitle("TODO");
      primaryStage.setResizable(false);
      primaryStage.show();
    } catch (Exception e) {
      System.out.println(1);
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    launch(args);
  }
}

my model class

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

public class todo {

  private SimpleIntegerProperty id;
  private SimpleStringProperty todo = null;
  private SimpleStringProperty description = null;
  private SimpleStringProperty delete = null;

  public todo(int id, String todo, String description, String delete) {
    this.id = new SimpleIntegerProperty(id);
    this.todo = new SimpleStringProperty(todo);
    this.description = new SimpleStringProperty(description);
    this.delete = new SimpleStringProperty(delete);
  }

  public Integer getId() {
    return id.get();
  }

  public String getTodo() {
    return todo.get();
  }

  public String getDescription() {
    return description.get();
  }

  public String getDelete() {
    return delete.get();
  }

  public void setId(int id) {
    this.id = new SimpleIntegerProperty(id);
  }

  public void setTodo(String todo) {
    this.todo = new SimpleStringProperty(todo);
  }

  public void setDescription(String description) {
    this.description = new SimpleStringProperty(description);
  }

  public void setDelete(String delete) {
    this.delete = new SimpleStringProperty(delete);
  }
}

my fxml file

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="468.0" prefWidth="692.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="todocontroller">
   <children>
      <TableView fx:id="table" layoutX="39.0" layoutY="216.0" prefHeight="229.0" prefWidth="614.0">
        <columns>
          <TableColumn fx:id="colId" maxWidth="70.0" prefWidth="70.0" text="id" />
          <TableColumn fx:id="colTodo" minWidth="100.0" prefWidth="100.0" text="todo" />
            <TableColumn fx:id="colDescription" minWidth="100.0" prefWidth="354.0" text="description" />
            <TableColumn fx:id="colDelete" minWidth="0.0" prefWidth="89.0" text="delete" />
        </columns>
      </TableView>
      <TextField fx:id="todo" layoutX="39.0" layoutY="94.0" prefHeight="25.0" prefWidth="200.0" promptText="ENTER TODO" />
      <TextArea fx:id="description" layoutX="39.0" layoutY="134.0" prefHeight="68.0" prefWidth="541.0" promptText="ENTER TODO DESCRIPTION" />
      <Button fx:id="add" layoutX="605.0" layoutY="177.0" mnemonicParsing="false" onAction="#add" prefHeight="25.0" prefWidth="48.0" text="ADD" />
      <Pane fx:id="header" prefHeight="85.0" prefWidth="692.0">
         <children>
            <Label fx:id="title" layoutX="38.0" layoutY="15.0" prefHeight="56.0" prefWidth="200.0" text="TODO APP">
               <font>
                  <Font name="Courier New" size="40.0" />
               </font>
            </Label>
         </children>
      </Pane>
   </children>
</AnchorPane>

my controllerclass

import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

public class todocontroller implements Initializable {

  @FXML
  TextField todo;

  @FXML
  TextArea description;

  @FXML
  static TableView<todo> table;

  @FXML
  public TableColumn<todo, Integer> colId;

  @FXML
  public TableColumn<todo, String> colTodo;

  @FXML
  public TableColumn<todo, String> colDescription;

  @FXML
  public TableColumn<todo, String> colDelete;

  ObservableList<todo> data = FXCollections.observableArrayList(
    new todo(0, "run", "go running", "ejhfue")
  );

  @Override
  public void initialize(URL location, ResourceBundle resource) {
    colId.setCellValueFactory(new PropertyValueFactory<todo, Integer>("id"));
    colTodo.setCellValueFactory(new PropertyValueFactory<todo, String>("todo"));
    colDescription.setCellValueFactory(
      new PropertyValueFactory<todo, String>("description")
    );
    colDelete.setCellValueFactory(new PropertyValueFactory<todo, String>("delete"));
    table.setItems(data); //this is the part that generate the error
  }
}```
YOFTAHE
  • 9
  • 1
  • 3
    [mcve] please, including the complete stacktrace formatted as code - aside: stick to java naming conventions when showing java code publicly – kleopatra May 25 '23 at 09:19
  • 3
    that said: injected fields _must not_ be static – kleopatra May 25 '23 at 09:21
  • 3
    The `LoadException` doesn't tell you, or us, what is actually wrong. I recommend reading [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/q/3988788/6395627), then you should [edit] your question to add the stack trace. Additionally, your setter methods in the `todo` class (whose name does not follow standard naming conventions) should not be creating new property objects. They should be setting the values of the existing property objects (e.g., `this.id.set(id)`). You're also missing the property-getter methods. – Slaw May 25 '23 at 09:53
  • 3
    Finally, any `@FXML`-annotated field must not be `static`. Given you say `table.setItems(data)` is where the error is being thrown, I can only assume you're getting a `NullPointerException`. And that's because you made the `table` field static. Static fields will not be injected, even when annotated with `@FXML` and there's a matching `fx:id` element in the FXML file. If you made the field static to make it accessible to other code, then that was the wrong approach. Look into using an architecture such as MVC or MVVM instead. – Slaw May 25 '23 at 09:54
  • 3
    As an aside, you might want to read [Why should I avoid using PropertyValueFactory in JavaFX?](https://stackoverflow.com/q/72437983/6395627) as well. – Slaw May 25 '23 at 09:59

0 Answers0