2
private void Label_GenerateMouseClicked(java.awt.event.MouseEvent evt) {                                            
    // TODO add your handling code here:
    try{
        String sql = "select from Account ORDER BY RAND()";
        PreparedStatement st = con.prepareStatement(sql);
        ResultSet rs = st.executeQuery();

        if(rs.next()){
            TextField_Username.setText(rs.getString("EMAIL"));
            TextField_Password.setText(rs.getString("PASSWORD"));
        }
    }catch(Exception e){
        System.out.println("Cannot generate.");
    }
}                                           

This code keep catching the error and i cant figure out why. im gettign error java.lang.NullPointerException. Please help

NgoCuong
  • 2,169
  • 1
  • 11
  • 9

3 Answers3

2

NVM GUYS I FIGURED IT OUT. I DIDNT RUN MY CONNECTION FUNCTION. srry for the troubles.

NgoCuong
  • 2,169
  • 1
  • 11
  • 9
  • 1
    Still, there are a number of other issues with your code, including the fact that your SQL statement is broken. See comments and additional answers. – Shotgun Ninja May 22 '15 at 17:44
0

Try this :

String sql = "select EMAIL,PASSWORD from Account ORDER BY RAND()";
        PreparedStatement st = con.prepareStatement(sql);
        ResultSet rs = st.executeQuery();

        if(rs.next()){
            TextField_Username.setText(rs.getString("EMAIL"));
            TextField_Password.setText(rs.getString("PASSWORD"));
        }
Ramesh Kotha
  • 8,266
  • 17
  • 66
  • 90
0

First off, your SQL statement is incorrect. Instead of

"select from Account ORDER BY RAND()"

it should be

"select Email, Password from Account ORDER BY RAND()"

When doing a select, you need to specify either the columns being selected, or use * to specify all tables. Production SQL code should never do a SELECT *, though; you should only specify which columns you're actually using to avoid leaking data and wasting time.

Also, I hope that Password column has its data encrypted...

Shotgun Ninja
  • 2,540
  • 3
  • 26
  • 32