0

Before you mark it as duplicate, please note that I had a look at the thread about null pointer exception we have on stackoverflow. Unfortunately, I have not managed to solve the problem and I would be grateful if someone could point me in right direction, since I am stuck for quite a while now.

I am getting an error

java.lang.NullPointerException at sample.LoginWindow.setBtnLogin(LoginWindow.java:39)) point at the following line preparedStatement.setString(1, txtUserName.getText()

I have class DBUtilities which holds my database connection and some closing methods:

 public class DBUtilities {
    // Getting connection to the database.
    public static Connection getConnection() throws SQLException {
        String url = "jdbc:mysql://localhost:3306/studentmanagementdb";
        String user = "admin";
        String pass = "administrator";
        Connection connection = DriverManager.getConnection(url, user, pass);
        return connection;
    }
    // Close prepared statement.
    public static void closePreparedStatement(PreparedStatement pStmt) {
        try {
            if(pStmt != null) {
                pStmt.close();
            }
        }catch(SQLException sExepction) {
            showErrorMsg("SQL Database Error:", "Prepared Statement could not be closed.");
        }
    }
    // Close result set.
    public static void closeResultSet(ResultSet resSet) {
        try {
            if (resSet != null){
                resSet.close();
            }
        }catch (SQLException sException) {
            showErrorMsg("SQL Database Error:", "Result Set could not be closed.");
        }
    }
    public static void closeConnection(Connection connect) {
        try {
            if(connect != null) {
                connect.close();
            }
        }catch (SQLException sException) {
            showErrorMsg("SQL Database Error:", "Database Connection could not be close.");
        }
    }
    // Shows error message.
    public static void showErrorMsg(String title, String msg) {
        Platform.runLater(() -> {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle(title);
            alert.setHeaderText(msg);
            alert.setWidth(200);
            alert.setHeight(200);
            alert.showAndWait();
        });
    }
}

Class which uses the connection:

public class LoginWindow implements Initializable{

    @FXML
    private TextField txtUserName;
    @FXML
    private PasswordField txtPassword;
    @FXML
    private Button btnLogin;

    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    DBUtilities dbUtil =  new DBUtilities();



    // Setting the login button.
    @FXML
    private void setBtnLogin(ActionEvent event) {

        try {
            connection = dbUtil.getConnection();
            String sqlQuery = "SELECT * FROM 'User_Login_Details' WHERE 'User_Name' = ? AND 'User_Password' = ?";
            connection.prepareStatement(sqlQuery);
            preparedStatement.setString(1, txtUserName.getText());
            preparedStatement.setString(2, txtPassword.getText());
            resultSet = preparedStatement.executeQuery();

        }catch (Exception exception) {
            //dbUtilities.showErrorMsg("Database Connection Error:", "Could not connect to the database.");
            exception.printStackTrace();
        }finally {
            dbUtil.closePreparedStatement(preparedStatement);
            dbUtil.closeResultSet(resultSet);
            dbUtil.closeConnection(connection);
        }
    }
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        btnLogin.setOnAction(this::setBtnLogin);
    }
}
Stefano Zanini
  • 5,876
  • 2
  • 13
  • 33

2 Answers2

1
preparedStatement = connection.prepareStatement(sqlQuery);

Creates a PreparedStatement object for sending parameterized SQL statements to the database.

SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
0

Initialize the prepared Statement as below

preparedStatement=connection.prepareStatement(sqlQuery);

Tushar M
  • 1
  • 1
  • Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Apr 07 '17 at 10:38