I'm wondering why it is causing a null pointer exception. I'm trying to trigger Connect() function from another class which should have connect me to the database.
MainController class:
DBConnect dbConnect;
@FXML private TextField logInField;
@FXML private PasswordField passwordField;
@FXML private Button logInButton;
@FXML private void buttonClicked() throws SQLException
{
dbConnect.Connect();
}
@FXML private void initialize(DBConnect dbConnect)
{
this.dbConnect=dbConnect;
}
DBConnection class:
Connection conn;
Statement st;
ResultSet rs;
public void Connect() throws SQLException
{
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookcase", "root", "1234");
if(conn.isValid(10))
{
System.out.println("Connection reached");
}
else
{
System.out.println("Something went wrong!");
}
}
It seems that the dbConnect property is not initialized, but i've already done it in initialize() method, which should be triggered after FXMLLoader loads the rest of the data, am I right? Am i using referance for the Connect() function from the other class correctly ? Regards