I am in doubt as to how to do a search in DB. He's going to catch. Does anyone know why? And how to solve it?
This is the function
public void consultar(){
String sql ="select * from tbaluno where idaluno=?";
try {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString(1, textid.getText());
rs = stmt.executeQuery();
if(rs.next())
System.out.println(textid);
if(rs.next()){
Usuario usuario = new Usuario();
usuario.setNomealuno(rs.getString("nomealuno"));
txtnome.setText(rs.getString(2));
}else{
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
The message that appears when I put an existing id in the database:
java.lang.NullPointerException
EDIT-> MySQL table, Contains:
ID | Name
1 | Vinicius
I wanted it when I put id 1, in the field name "Vinicius". The change I made in the function see above, was to just remove an if and change the other by while.