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.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class IPS_Application extends Application {
    @Override
    public void start(Stage stage) {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(IPS_Application.class.getResource("IPS.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 520, 640);
            stage.setTitle("MyProgram");
            stage.setResizable(false);
            stage.setScene(scene);
            stage.show();

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

    public static void main(String[] args) {
        launch(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="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.ips.SecondWindow_Controller">
   <children>
      <TabPane fx:id="TABBBB" prefHeight="567.0" prefWidth="907.0" tabClosingPolicy="UNAVAILABLE">
         <tabs>
            <Tab fx:id="TAB1" text="Карта">
               <content>
                  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="539.0" prefWidth="1042.0">
                     <children>
                        <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">
                          <columns>
                            <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" />
                          </columns>
                        </TableView>
                        <Label fx:id="PS_L" layoutX="526.0" layoutY="14.0" text="Поиск по индексу">
                           <font>
                              <Font size="18.0" />
                           </font>
                        </Label>
                        <TextField fx:id="TXF_1" layoutX="683.0" layoutY="16.0" prefHeight="26.0" prefWidth="276.0" promptText="Введите числа индекса" />
                     </children>
                  </AnchorPane>
               </content>
            </Tab>
            <Tab fx:id="TAB2" text="Анимация">
               <content>
                  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <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="Заново" />
                     </children>
                  </AnchorPane>
               </content>
            </Tab>
            <Tab text="ОТЕ">
              <content>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
              </content>
            </Tab>
         </tabs>
      </TabPane>
   </children>
</VBox>

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.net.URL;
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;

    @FXML
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");
    stage.setScene(scene);
    stage.show();
  }
  catch(Exception e)
  {
    System.err.println(e.getMessage());
  }
}

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

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

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
       loadDate();

        }


    private void Colu_1 () throws SQLException {
        try {
            Soil_ConditionList.clear();

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

            while (resultSet.next()) {

                Soil_ConditionList.add(new Soil_Condition(
                        resultSet.getInt("Music"),
                        resultSet.getString("Soil_Condition"),
                        resultSet.getString("Agro"),
                        resultSet.getString("Secure")));

                Colu_1.setItems(Soil_ConditionList);

        }

        } 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
javafx.fxml.LoadException: 
/C:/Users/Acer/IPS/target/classes/main/ips/IPS.fxml

    at javafx.fxml@18/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
    at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2685)
    at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at javafx.fxml@18/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2516)
    at main.ips/main.ips.IPS_Application.start(IPS_Application.java:17)
    at javafx.graphics@18/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics@18/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics@18/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics@18/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics@18/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics@18/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics@18/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:833)
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(IPS_Controller.java:323)
    at main.ips/main.ips.IPS_Controller.initialize(IPS_Controller.java:283)
    at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2655)
    ... 12 more

Leave a Comment