Populating a TableView with data from Database JavaFX


I'm trying to populate my TableView in JavaFX with data from my database. I've been trying a lot of different methods to try and get something working. I've been changing a lot of things but ultimately I haven't gotten anywhere. I think it has something to do with the way I'm loading my FXML file.

My database has 3 columns, so I'm not sure if I have to define everything from the database in my Artist class and that's why it's giving me errors.

I built my GUI with SceneBuilder so I'll attach my FXML file.

My main class

package application;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;

public class Main extends Application {
public void start(Stage stage) throws IOException {
    FXMLLoader loader = new FXMLLoader(Main.class
        Group rootGroup = loader.load();

    final artistSelectionController controller = loader.getController();
    Scene scene = new Scene(rootGroup,600,400);
    StageStyle stageStyle = StageStyle.TRANSPARENT;

public static void main(String[] args) {

My Controller Class

public class artistSelectionController{

private Rectangle rect;

private ImageView logo;

private HBox buttons;

private Button newArtist;

private Button closeScreen;

private Stage stage;
private double dragAnchorX;
private double dragAnchorY;

private PreparedStatement preparedStatement;

public void setStage(Stage stage){
    this.stage = stage;

public void closeScreenHandler(ActionEvent e){

public void mousePressedHandler(MouseEvent me) {
    dragAnchorX = me.getScreenX() - stage.getX();
    dragAnchorY = me.getScreenY() - stage.getY();

public void mouseDraggedHandler(MouseEvent me) {
    stage.setX(me.getScreenX() - dragAnchorX);
    stage.setY(me.getScreenY() - dragAnchorY);

private static TableView<Artist> artistTable;

private TableColumn<Artist, String> artistName;

private DBConnect objDbClass;

private Connection con;

private Logger logger;

void initialize(){
    // assert artistTable != null : "fx:id=\"tableview\" was not injected: check your FXML file 'UserMaster.fxml'.";
        new PropertyValueFactory<Artist,String>("Artist"));        

    objDbClass = new DBConnect();
        con = objDbClass.getConnection();
    catch(ClassNotFoundException ce){
    catch(SQLException ce){

private ObservableList<Artist> data;

public void buildData(){        
    data = FXCollections.observableArrayList();
        String SQL = "Select Name from ARTIST";            
        ResultSet rs = con.createStatement().executeQuery(SQL);  
            Artist cm = new Artist();                     


    catch(Exception e){
          System.out.println("Error on Building Data");            



package application;
import javafx.beans.property.SimpleStringProperty;

public class Artist {

public SimpleStringProperty name =  new SimpleStringProperty();
public SimpleStringProperty genre = new SimpleStringProperty();

public String getName() {
    return name.get();

public String getGenre() {
    return genre.get();

My Database Connector

import java.sql.*;

public class DBConnect{    
public Connection getConnection() throws ClassNotFoundException, SQLException{       
      return DriverManager.getConnection("jdbc:mysql://localhost/myStudio","root","root"); 

And finally my FXML file

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

<?import javafx.scene.control.*?>
<?import javafx.scene.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.shape.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<Group fx:id="rootGroup" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application/artistSelectionController">
  <Rectangle fx:id="rect" arcHeight="50.0" arcWidth="50.0" height="376.0" layoutX="13.0" layoutY="13.0" stroke="BLACK" strokeType="INSIDE" width="577.0">
        <LinearGradient cycleMethod="REPEAT" endX="1.0" endY="1.0" startY="1.0">
              <Stop color="#1b2f36" />
              <Stop color="#1b2f36" offset="0.007662835249042145" />
              <Stop color="#15596e" offset="1.0" />
  <ImageView fx:id="logo" fitHeight="411.0" fitWidth="606.0" layoutX="13.0" layoutY="13.0" pickOnBounds="true" preserveRatio="true">
        <Image url="@../../../../Downloads/myStudio.png" />
  <TableView fx:id="artistTable" layoutX="316.0" layoutY="143.0" prefHeight="223.0" prefWidth="241.0">
        <TableColumn fx:id="artistName" editable="false" prefWidth="240.0" resizable="false" text="Artists" />
  <HBox fx:id="buttons" layoutX="65.0" layoutY="339.0" prefHeight="100.0" prefWidth="200.0" spacing="20.0">
        <Button fx:id="newArtist" mnemonicParsing="false" prefWidth="75.0" text="New Artist" />
        <Button fx:id="closeScreen" mnemonicParsing="false" prefWidth="75.0" text="Close" />


11/17/2014 7:15:41 PM

Accepted Answer

There seem to be many errors in your code.

First, you have made artistTable static. Don't do this - it makes no sense at all, and static fields won't be injected by the FXMLLoader. This is why you are getting the null pointer exception.

Second, the value you pass to the PropertyValueFactory ("Artist") does not match the name of any property in your model class Artist. (I.e. there is no ArtistProperty() method and no getArtist() method).

You probably want

        new PropertyValueFactory<Artist,String>("name"));

which will tie the value for the column to the getName() method. See the PropertyValueFactory documentation for a description of how it works.

(You should probably define your POJO according to the JavaFX Property pattern described here. However just having a get...() method will work.)

11/17/2014 8:06:38 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow