#Variable is null even when value is inputted

1 messages · Page 1 of 1 (latest)

idle glenBOT
#

This post has been reserved for your question.

Hey @tired python! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

tired python
#

MainController Class:


import javafx.fxml.FXML;
import javafx.scene.control.Label;

import javax.xml.transform.Result;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MainController {
    private Connection con;

    public MainController(){

    }
    public MainController(Connection con) throws SQLException {
        this.con = con;
        if(con == null){
            System.out.println("Connection null in Constructor");
        }
        else {
            System.out.println("Connection is not null in Constructor");
        }

    }

    @FXML
    public void confirm() throws IOException {
        Main.setRoot("scan-event");
    }

    @FXML
    public void checkConnection() throws SQLException {
        if(con == null){
            System.out.println("Connection null in checkConnection()");
        }
        else {
            System.out.println("Connection is not null in checkConnection()");
        }
    }
}```
#

so what Im current doing is that Im trying to make a inventory system that involves databases like mySQL

#

now the problem that arrived is that Im having a hard time initializing the Connection variable to other classes so I would be able to create a statement.

#

But everytime I try, the con variable is always null. I dont know why

#

in the checkConnection method. The con variable is null even tho I already checked in the Constructor of the class that the con variable is not null.

Aug 28, 2023 12:18:05 AM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 20.0.1 by JavaFX runtime of version 20
Connection null in checkConnection()```