0

I'm starting to learn javaFX and I need to populate a table with data from my database. I've tried to understand different ways of doing but keep getting errors

(Caused by: java.lang.NullPointerException

at common.logic.RepairContoller.filltable(RepairController.java:81)

common.logic.RepairContoller.initializable(ReepairController.java:54)).

This is where im up to can anyone help met alter this or give any advice:

My Contoller Class

package common.logic;

import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class RepairController implements Initializable {

private ArrayList<Booking> bookings = new ArrayList<>();

@FXML public TableView <Booking> bookingTable;
@FXML public TableColumn<Booking, Integer> id;
@FXML public TableColumn<Booking,String> vReg;
@FXML public TableColumn<Booking, String> mechanic;
@FXML public TableColumn<Booking, Integer> duruation;
@FXML public TableColumn<Booking, String> bType;
@FXML public TableColumn<Booking, String> Date;
@FXML public TableColumn<Booking, String> SPC;
@FXML public TableColumn<Booking, Integer> Part;
@FXML public TableColumn<Booking, String> complete;

@FXML public Button editbooking1;
@FXML public Button addbooking1;
@FXML public Button deletebooking1;

@FXML private TextField search_bar;


private databconenction dbc = databconenction.getInstance();
private static Booking selected;

public void initialize(URL location, ResourceBundle resourceBundle)
{
    fillTable();
    search_bar.textProperty().addListener((ob, oldVal, newVal) -> Search());
    String sql = "SELECT *  FROM Booking";
    ResultSet rsb = dbc.query(sql);
    try {
        while (rsb.next())
        {
            bookings.add(new Booking(rsb));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

}
private void Search() {
}


public void fillTable()
{
    setValueFactories();
    ArrayList<Booking> tableValues = new ArrayList<>();
    String sql = "SELECT *  FROM Booking";
    ResultSet rsb = dbc.query(sql);
    try {
        while (true)
        {
            if (!(rsb.next())) break;
            tableValues.add(new Booking(rsb));
        }
        bookingTable.setItems(FXCollections.observableArrayList(tableValues));
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

private void setValueFactories() {
    id.setCellValueFactory(new PropertyValueFactory<>("id"));
    vReg.setCellValueFactory(new PropertyValueFactory<>("vReg"));
    mechanic.setCellValueFactory(new PropertyValueFactory<>("mechanic"));
    duruation.setCellValueFactory(new PropertyValueFactory<>("duruation"));
    bType.setCellValueFactory(new PropertyValueFactory<>("bTtype"));
    Date.setCellValueFactory(new PropertyValueFactory<>("Date"));
    SPC.setCellValueFactory(new PropertyValueFactory<>("SPC"));
    Part.setCellValueFactory(new PropertyValueFactory<>("Part"));
    complete.setCellValueFactory(new PropertyValueFactory<>("complete"));

}

@FXML

public void addB() {
    System.out.println("You want to add a booking");
    try {
        Stage stage = new Stage();
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample/add.fxml"));
        Parent root1 = fxmlLoader.load();
        stage.setScene(new Scene(root1));
        stage.show();

        }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}
 @FXML

public void editB(ActionEvent f) {
System.out.println("You want to edit a booking");
try {
    Stage stage = new Stage();
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample/edit.fxml"));
    Parent root2 = fxmlLoader.load();
    stage.setScene(new Scene(root2));
    stage.show();

}
catch (IOException a)
{
    a.printStackTrace();
}
}
 @FXML
public void deleteB(ActionEvent d) {
    System.out.println("You want to delete a booking");
       try {
        Stage stage = new Stage();
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample/delete.fxml"));
        Parent root3 = fxmlLoader.load();
        stage.setScene(new Scene(root3));
        stage.show();

    }
    catch (IOException ie)
    {
        ie.printStackTrace();
    }

}


}

Database connection class

package common.logic;

import java.sql.DriverManager ;
import java.sql.*;
import java.sql.Connection;
import java.sql.ResultSet ;

public class databconenction {

private static final databconenction INSTANCE = new databconenction();
public Connection con = null;

public databconenction() { connect();}

public static databconenction getInstance() { return INSTANCE;}

public Connection connect()
{
    if(con !=null)
        return con;
    try
    {
        Class.forName("org.sqlite.JDBC");
        con = DriverManager.getConnection("JDBC:sqlite:C:\\Users\\Don\\Documents\\SE25-test\\scratch\\Donovan\\sample\\GM_SIS_02.db");
    } catch (ClassNotFoundException e)
                    {
        System.out.println("Driver not found!");
        e.getException();
    } catch (SQLException se){
        System.out.println("NO CONNECTION");
        se.getMessage();
    }
    return con;
}
public ResultSet query(String sql)
{
    ResultSet rs = null;
    try
    {
        Statement stmt = con
                .createStatement();
        rs = stmt.executeQuery(sql);
    }
    catch (SQLException se)
    {
        se.printStackTrace();
    }
    return rs;
}

public void update(String sql){
    try{
        Statement stmt = con.createStatement();
        stmt.executeUpdate(sql);
    } catch (SQLException se){
        se.printStackTrace();
    } catch (NullPointerException ignored){
    }
}


}

GUI FXML

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<Pane minHeight="0.0" minWidth="0.0" prefHeight="507.0" prefWidth="897.0"        xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1"   fx:controller="common.logic.RepairController">
 <children>
    <TableView fx:id="customerTable" layoutX="12.0" layoutY="170.0"   prefHeight="267.0" prefWidth="877.0">
        <columns>
            <TableColumn fx:id="id" prefWidth="97.4" text="Booking ID" />
            <TableColumn fx:id="vReg" prefWidth="97.4" text="Vehicle Reg" />
            <TableColumn fx:id="mechanic" prefWidth="97.4" text="Mechanic"     />
            <TableColumn fx:id="duruation" prefWidth="97.4" text="Duruation" />
            <TableColumn fx:id="bType" prefWidth="97.4" text="Booking Type" />
            <TableColumn fx:id="Date" prefWidth="97.4" text="Date" />
            <TableColumn fx:id="SPC" prefWidth="97.4" text="SPC" />
            <TableColumn fx:id="Part" prefWidth="97.4" text="Part" />
         <TableColumn fx:id="complete" prefWidth="97.4" text="Complete" />

        </columns>
    </TableView>
    <Text layoutX="22.0" layoutY="95.0" strokeType="OUTSIDE"  strokeWidth="0.0" text="Diagnosis and Repairs / Scheduled Maintenance">
        <font>
            <Font name="Cambria" size="36.0" />
        </font>
    </Text>

    <Button layoutX="757.0" layoutY="109.0" mnemonicParsing="false" text="Enter" />
    <Button fx:id="addbooking1" layoutX="22.0" layoutY="454.0" mnemonicParsing="false" onAction="#addB" text="Add" />
    <Button fx:id="editbooking1" layoutX="126.0" layoutY="454.0" mnemonicParsing="false" onAction="#editB" text="Edit" />
    <Button fx:id="deletebooking1" layoutX="229.0" layoutY="454.0" mnemonicParsing="false" onAction="#deleteB" text="Delete" />
   </children>
 </Pane>
  • "Keep getting errors". What errors? Please [edit] your question to include the [stack trace](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors). – James_D Mar 16 '17 at 12:20
  • Also, be advised that issuing non-UI calls inside the UI thread (JavaFX Application Thread) is not a good idea. Make sure that all database access calls are executed on a background thread, update the `TableView` inside the UI thread. – Andres Almiray Mar 16 '17 at 12:27
  • Just my $0.02 worth: I would not try to add multithreading until you are comfortable with the actual basics of retrieving data and using the JavaFX framework. There are more fundamental problems with this code than UI responsiveness atm (not closing db resources, duplicating data in the UI class, etc etc). – James_D Mar 16 '17 at 12:42
  • edited thanks @James_D – Donovan Haynes Mar 16 '17 at 12:42
  • Can you put the *complete* stack trace in? Format it as code. Which line is line 81? – James_D Mar 16 '17 at 12:43
  • Once you identify the line that is the culprit, it should be easy to figure out which variable is `null`. (Read [this](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) if you haven't read it before. Actually, read it again anyway.) From there it will be easy to figure out the problem. – James_D Mar 16 '17 at 12:51
  • sorry new to the site not sure how to do that but line 54 is fillTable(); and line 81 is if (!(rsb.next())) break; – Donovan Haynes Mar 16 '17 at 12:53
  • [Edit] the question. Copy the entire stack trace and paste it in. [Format it as a block of code](https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks). – James_D Mar 16 '17 at 12:53

0 Answers0