0

Been getting an error at the line where I used "while (this.resultSet.next())" I'm new to javafx, still studying and watching youtube tutorials, unfortunately I can't find a video on how to fix this. The data from my database wont populate the table

Here's the error:
Caused by: java.lang.NullPointerException at sample.TableViewController.Save(TableViewController.java:73) ... 120 more

 package sample;

import com.mysql.jdbc.ResultSet;
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.cell.PropertyValueFactory;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TableViewController implements Initializable {

    @FXML
    private TableView<ModelTable> table;

    @FXML
    private TableColumn<ModelTable, String> ID;

    @FXML
    private TableColumn<ModelTable, String> FrstName;

    @FXML
    private TableColumn<ModelTable, String> LstName;

    @FXML
    private TableColumn<ModelTable, String> GS;

    @FXML
    private TableColumn<ModelTable, String> Grade1;

    @FXML
    private TableColumn<ModelTable, String> Grade2;

    @FXML
    private TableColumn<ModelTable, String> Grade3;

    @FXML
    private TableColumn<ModelTable, String> Grade4;

    @FXML
    private TableColumn<ModelTable, String> Grade5;

    @FXML
    private TableColumn<ModelTable, String> Grade6;

    Connection con = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    ObservableList<ModelTable> oblist = FXCollections.observableArrayList();

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

    }

    public void Save(ActionEvent event) {
        try {
            String query = "Select * from `records`";
            con = DriverManager.getConnection("jdbc:mysql://localhost/records", "root", "");
            preparedStatement = con.prepareStatement(query);
            preparedStatement.executeQuery();
            while (this.resultSet.next()) {
                oblist.add(new ModelTable(resultSet.getString("ID"), resultSet.getString("FrstName"), resultSet.getString("LstName"), resultSet.getString("GS"), resultSet.getString("Grade1"), resultSet.getString("Grade2"), resultSet.getString("Grade3"),resultSet.getString("Grade4"), resultSet.getString("Grade5"), resultSet.getString("Grade6")));
            }

            ID.setCellValueFactory(new PropertyValueFactory<>("ID"));
            FrstName.setCellValueFactory(new PropertyValueFactory<>("First Name"));
            LstName.setCellValueFactory(new PropertyValueFactory<>("Last Name"));
            GS.setCellValueFactory(new PropertyValueFactory<>("Grade/Section"));
            Grade1.setCellValueFactory(new PropertyValueFactory<>("Grade1"));
            Grade2.setCellValueFactory(new PropertyValueFactory<>("Grade2"));
            Grade3.setCellValueFactory(new PropertyValueFactory<>("Grade3"));
            Grade4.setCellValueFactory(new PropertyValueFactory<>("Grade4"));
            Grade5.setCellValueFactory(new PropertyValueFactory<>("Grade5"));
            Grade6.setCellValueFactory(new PropertyValueFactory<>("Grade6"));


            table.setItems(oblist);

        } catch (SQLException ex) {
            Logger.getLogger(TableViewController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
  • You never initialize `resultSet` so it's `null` when you attempt to use it. You probably wanted `resultSet = preparedStatement.executeQuery()`. – Slaw Feb 18 '20 at 09:52
  • Some other notes: **(1)** Please follow [Java naming conventions](https://google.github.io/styleguide/javaguide.html#s5-naming); **(2)** You're opening a new `Connection` every invocation of `save` without closing the old one; **(3)** You may want to cache and reuse the `Connection` **(4)** Making the `ResultSet` a field doesn't really make sense as it should be closed when done with it; **(5)** Same possibly goes for the `PreparedStatement`; **(6)** You only need to set the `cellValueFactory` once during initialization; – Slaw Feb 18 '20 at 10:03
  • 1
    **(7)** `new PropertyValueFactory<>("Gradle/Section")` can't work because `/` cannot be used in a Java identifier, meaning your property can't possibly have that name. **(8)** Your executing database calls on the _JavaFX Application Thread_ but it'd be better to use a background thread—see [Concurrency in JavaFX](https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm); **(9)** It'd be better from a design sense to move the database access code into another class. – Slaw Feb 18 '20 at 10:04
  • It somehow works now, thanks But now theres another problem it says "Can not retrieve property 'Last Name' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@52503314 with provided class type: class sample.ModelTable" This is the same for every PVF I used except for one, which was the Id and this too "java.lang.IllegalStateException: Cannot read from unreadable property Last Name" – Al-sharief Dinglasa Feb 18 '20 at 10:46
  • 1
    Should have mentioned that neither `new PropertyValueFactory<>("First Name")` nor `new PropertyValueFactory<>("Last Name")` can work either since spaces are not allowed in Java identifiers, meaning your properties can't possibly have those names. Can't speak to the `"Grade1"` and similar names as those are valid identifiers, but that doesn't mean your model has the necessary properties. See https://openjfx.io/javadoc/13/javafx.controls/javafx/scene/control/cell/PropertyValueFactory.html, https://stackoverflow.com/a/17037130/6395627, and https://stackoverflow.com/q/16377820/6395627. – Slaw Feb 18 '20 at 10:59
  • Does that mean my sql database should also not include spaces? im sorry – Al-sharief Dinglasa Feb 18 '20 at 11:01
  • MySQL, and SQL in general, is its own thing. What I'm addressing is solely the JavaFX side of things. That said, as far as I know having spaces in database table/column names is not conventional. I would expect `first_name` and `last_name` as the database column names. – Slaw Feb 18 '20 at 11:05

0 Answers0