Search the database

-1

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.

    
asked by anonymous 18.04.2018 / 04:42

1 answer

1

Probably in your table there are more things than nomealuno one of them should be idaluno , which comes before.

Once the program runs it passes the single if % effective (although there has one more) and can not access the column name, generating a nullpointer.

Try changing if for while(rs.next()) since you select all attributes in select. Or change the select to only name

    
18.04.2018 / 09:02