JavaFX program with MySQL isn’t working because “this.Music” is null

Need help! I don’t quite understand what’s wrong with my code – can someone point it out to me, please?

I am working on JavaFX, creating a program with two windows. In the second window, there is a table with columns. I created my database in MySQL, added some data to it, and it works pretty well. I used mysql-connector-java-8.0.29 to connect these two. Also, it should be noted that I wrote all the code in my First Controller (the Controller of the first window), and not in the Second Controller.

I wrote the code that would’ve displayed my table with some data on my program. But I’m getting the same mistake again and again: ” java.lang.NullPointerException: Cannot invoke “javafx.scene.control.TableColumn.setCellValueFactory(javafx.util.Callback)” because “this.Music” is null “

I tried everything on the Internet and searched for some similar problems but nothing helps. I noticed if I change the variable “Music” to “#Music” my program at least runs: only the UI of my Table without any MySQL Database data. java.lang.NullPointerException says

Music:setCellValueFactory(new PropertyValueFactory<>("Music")); in IPS_Controller.loadDate() (filter:null)
private TableColumn<Soil_Condition, Integer> Music; in IPS_Controller

But what does it mean? What should I change?

Main Class:

package main.ips;

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

import java.util.logging.Level;
import java.util.logging.Logger;

public class IPS_Application extends Application {
    public void start(Stage stage) {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(IPS_Application.class.getResource("IPS.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 520, 640);

        } catch (IOException ex) {
            Logger.getLogger(IPS_Application.class.getName()).log(Level.SEVERE, null, ex);

    public static void main(String[] args) {

Database Connection:

package main.ips;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DbConnect {

    private static String HOST = "localhost";
        private static int PORT = 3306;
        private static String DB_NAME = "MyIPS";
        private static String USERNAME = "root";
        private static String PASSWORD = "password";
        private static Connection connection;

    public static Connection getConnect () {
        try {
            connection = DriverManager.getConnection(String.format("jdbc:mysql://localhost:3306/", HOST, PORT, DB_NAME), USERNAME, PASSWORD);

        } catch (SQLException ex) {
            Logger.getLogger(DbConnect.class.getName()).log(Level.SEVERE, null, ex);

        return connection;



Oil Condition Class:

package main.ips;

public class Soil_Condition {
    Integer Music;
    String Soil_Condition, Agro, Secure;

    public Soil_Condition (Integer music, String soil_Condition, String agro, String secure) {
        this.Music = music;
        this.Soil_Condition = soil_Condition;
        this.Agro = agro;
        this.Secure = secure;

    public Integer getMusic() {
        return Music;

    public String getSoil_Condition() {
        return Soil_Condition;

    public String getAgro() {
        return Agro;

    public String getSecure() {
        return Secure;

    public void setMusic(Integer music) {
        Music = music;

    public void setSoil_Condition(String soil_Condition) {
        Soil_Condition = soil_Condition;

    public void setAgro(String agro) {
        Agro = agro;

    public void setSecure(String secure) {
        Secure = secure;

Second Window FXML:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Rectangle?>
<?import javafx.scene.text.Font?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="570.0" prefWidth="980.0" xmlns="" xmlns:fx="" fx:controller="main.ips.SecondWindow_Controller">
      <TabPane fx:id="TABBBB" prefHeight="567.0" prefWidth="907.0" tabClosingPolicy="UNAVAILABLE">
            <Tab fx:id="TAB1" text="Карта">
                  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="539.0" prefWidth="1042.0">
                        <Rectangle fx:id="box11" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="500.0" layoutX="18.0" layoutY="12.0" stroke="BLACK" strokeType="INSIDE" width="500.0" />
                        <TableView fx:id="Colu_1" layoutX="525.0" layoutY="53.0" prefHeight="458.0" prefWidth="434.0">
                            <TableColumn fx:id="Music" prefWidth="75.0" text="Music" />
                            <TableColumn fx:id="S_C" prefWidth="116.79998779296875" text="Soil_Condition" />
                              <TableColumn fx:id="Agro" prefWidth="146.933349609375" text="Agro" />
                              <TableColumn fx:id="SEC" minWidth="0.0" prefWidth="91.4666748046875" text="Secure" />
                        <Label fx:id="PS_L" layoutX="526.0" layoutY="14.0" text="Поиск по индексу">
                              <Font size="18.0" />
                        <TextField fx:id="TXF_1" layoutX="683.0" layoutY="16.0" prefHeight="26.0" prefWidth="276.0" promptText="Введите числа индекса" />
            <Tab fx:id="TAB2" text="Анимация">
                  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                        <Rectangle fx:id="box2222" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="447.0" layoutX="14.0" layoutY="16.0" stroke="BLACK" strokeType="INSIDE" width="952.0" />
                        <Button fx:id="btnbtn" layoutX="160.0" layoutY="479.0" mnemonicParsing="false" text="Начать" />
                        <Button fx:id="btnbtnbtn" layoutX="759.0" layoutY="480.0" mnemonicParsing="false" text="Заново" />
            <Tab text="ОТЕ">
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />

First Controller with all the code:

package main.ips;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

public class IPS_Controller implements Initializable {

    @FXML private TableColumn<Soil_Condition, Integer> Music;
    @FXML private TableColumn<Soil_Condition, String> Agro;
    @FXML private TableView<Soil_Condition> Colu_1;
    @FXML private TableColumn<Soil_Condition, String> SEC;
    @FXML private TableColumn<Soil_Condition, String> S_C;
    @FXML private MenuBar to;
    @FXML private TextArea textArea;

void handleButton1Action(ActionEvent event) {
  try {
    FXMLLoader fxmlLoader = new FXMLLoader(IPS_Application.class.getResource("SECONDWINDOW.fxml"));
    Scene scene = new Scene(fxmlLoader.load(), 980, 570);
    Stage stage = new Stage();
    stage.setTitle("Second Window");
  catch(Exception e)

    String query = null;
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    Soil_Condition soil_condition = null;

    ObservableList<Soil_Condition> Soil_ConditionList = FXCollections.observableArrayList();

    public void initialize(URL arg0, ResourceBundle arg1) {


    private void Colu_1 () throws SQLException {
        try {

            query = "SELECT * FROM Soil_Condition;";
            preparedStatement = connection.prepareStatement(query);
            resultSet = preparedStatement.executeQuery();

            while ( {

                Soil_ConditionList.add(new Soil_Condition(



        } catch (SQLException ex) {
            Logger.getLogger(IPS_Controller.class.getName()).log(Level.SEVERE, null, ex);


    private void loadDate() {
        connection = DbConnect.getConnect();

        Music.setCellValueFactory(new PropertyValueFactory<>("Music"));
        S_C.setCellValueFactory(new PropertyValueFactory<>("Soil_Condition"));
        Agro.setCellValueFactory(new PropertyValueFactory<>("Agro"));
        SEC.setCellValueFactory(new PropertyValueFactory<>("Secure"));

That’s what I’m constantly getting:

���. 12, 2022 12:45:22 AM main.ips.IPS_Application start
SEVERE: null

    at javafx.fxml@18/javafx.fxml.FXMLLoader.constructLoadException(
    at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(
    at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(
    at javafx.fxml@18/javafx.fxml.FXMLLoader.load(
    at main.ips/main.ips.IPS_Application.start(
    at java.base/
    at Method)
    at java.base/
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.TableColumn.setCellValueFactory(javafx.util.Callback)" because "this.Music" is null
    at main.ips/main.ips.IPS_Controller.loadDate(
    at main.ips/main.ips.IPS_Controller.initialize(
    at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(
    ... 12 more

Leave a Comment