JavaFX with MySql login form - NullPointerException


Question

I am trying to make login form with java and mysql. I have user database with one table, and userName and password field. I have my base UserBase class and my LoginFXMLController class. When I run it I get NullPointerException. When I debug it it shows null value on ps=conn.prepareStatment.

Here is my LoginFXMLController class:

public class LoginFXMLController implements Initializable {

@FXML
private Button okBtn;
@FXML
private Button exitBtn;
@FXML
private Label infoPassword;
@FXML
private TextField nameFld;
@FXML
private PasswordField passwordFld;

private Connection conn = null;
private PreparedStatement ps = null;
private ResultSet rs = null;

/**
 * Initializes the controller class.
 */
public void initialize(URL url, ResourceBundle rb) {
     conn=  UserBase.get();

}

// Akcija za ok dugme u login formi
public void okBtnAction(ActionEvent event) throws SQLException, IOException {
    String userName = nameFld.getText().trim();
    String password = passwordFld.getText().trim();

    String sql = "SELECT * user WHERE userName = ? AND password = ?";
    try {
        ps = conn.prepareStatement(sql);
        ps.setString(1, userName);
        ps.setString(2, password);
        rs = ps.executeQuery();

        if (rs.next()) {
            Parent root = FXMLLoader.load(getClass().getResource("CoreAppFXML.fxml"));
            Stage stage = new Stage();
            stage.setScene(new Scene(root));
            stage.setTitle("Main");
            stage.show();

            Stage login = (Stage) exitBtn.getScene().getWindow();
            login.close();
        } else {
            wrongInputFXML();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

public void wrongInputFXML() throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource(
            "WrongUsernameFXML.fxml"));
    Stage errorStage = new Stage();
    errorStage.setScene(new Scene(root));
    errorStage.setTitle("Error");
    errorStage.centerOnScreen();
    errorStage.show();
}

// Akcija za exit dugme u login formi
public void exitBtnAction(ActionEvent event) {
    Stage stage = (Stage) exitBtn.getScene().getWindow();
    stage.close();
}
}

And my UserBase class:

public class UserBase {

private static Connection connection;

// Pristupa drajveru u JAR fajlu
private static Connection createConnection() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        return DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/user", "root", "");
    } catch (Exception e) {
        return null;
    }
}

public static Connection get() {
    if (connection == null) {
        connection = createConnection();
    }
    return connection;
}

I am beginner in this so take it easy.

1
0
8/15/2014 10:30:00 AM

@FXML
public void login(ActionEvent event) {
    int count = 0;
    String Email1 = txtemaillogin.getText().trim();
    String Password1 = txtpasslogin.getText().trim();
        String query = "SELECT * FROM companyinfo";
    try {
        ps = connection.prepareStatement(query);   
        rs = ps.executeQuery(query);
        count++;
        boolean check = false;
        while(rs.next()){

            String user = rs.getString("Email");
            String pass = rs.getString("Password");

            if(user.equals(Email1) && pass.equals(Password1)){
                check = true;
                ps.close();
                JOptionPane.showMessageDialog(null, "Login Successfully");

                btn_login.getScene().getWindow().hide();

                Stage stage = new Stage();
                Parent root1 = 
                FXMLLoader.load(getClass().getResource("/Home/homeUI.fxml"));
                //stage.initStyle(StageStyle.UNDECORATED);
                Scene scene = new Scene(root1);
                stage.initStyle(StageStyle.UNDECORATED);
                stage.setScene(scene);
                stage.show();

                loginpane.setVisible(Boolean.FALSE);
            }

        }
        if(check == false){
            JOptionPane.showMessageDialog(null, "Login Fail");
        }
        else if(count>3){ 
            JOptionPane.showMessageDialog(null, "Login Denied");
        }
1
6/26/2018 5:57:14 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon