0

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.

Aubin
  • 14,617
  • 9
  • 61
  • 84
tadija
  • 23
  • 3
  • 6
  • A stack trace of the NullPointerException would be helpful in pinpointing the error. – kviiri Aug 15 '14 at 10:34
  • add some trace and/or breakpoint in initialize, I'm not sure you go in it – Aubin Aug 15 '14 at 10:36
  • >No current context (stack frame)< - if this is what you meant. If not can you tell me where can I find it? Also got this >"ps" is not a known variable in the current context. – tadija Aug 15 '14 at 10:38
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Mark Rotteveel Aug 15 '14 at 10:58
  • it saying in this line ps = conn.prepareStatement(sql); that conn = null.. What am I doing wrong? Can someone please help me with this? – tadija Aug 15 '14 at 12:30
  • Put an `e.printStackTrace()` in the `catch` block in the `createConnection` method. It looks like you are getting an exception there. – James_D Aug 15 '14 at 12:32
  • com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'user WHERE userName = '' AND password = ''' at line 1 this is returned – tadija Aug 15 '14 at 13:25
  • 1
    Its fixed. I didn't import jdbc driver into library. This was 2nd error and 1st was ClassNotFoundException: com.mysql.jdbc.Driver. It is working now, thank you all. – tadija Aug 15 '14 at 13:58

2 Answers2

1
@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");
        }
0
    /* Please Import Ur DataBase Class */
    import UserBase;


    public class LoginFXMLController implements Initializable {

// Create Constructor  
public LoginFXMLController ()
{
 // initialize connection
         conn= createConnection();
}
    @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) {


    }

    // 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();
    }
    }

    /* Change 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;
        }
    }