Arnas Arnelis February 2016

Getting data from JTable to jText fields, but i'm getting Null Pointer Exception

i'm trying to get some values from jTable (the values in JTable are from MySQL) to JTextField.

I got "Null Pointer Exception" after selecting the row.

After i put Line BreakPoint i see that row number is get. But at the rs=ppst.executeQuery(); line it shows me NullPointerException.

What's wrong?

here is my code:

private void jTableStudentaiMouseClicked(java.awt.event.MouseEvent evt) {                                             

       try{
            int row = jTableStudentai.getSelectedRow();
            String Table_click = (jTableStudentai.getModel().getValueAt(row, 0).toString());

            String sql = "select * from studentas inner join grupė on studentas.GrupėsID=grupė.GrupėsID where StudentoNr ='"+Table_click+"'";
       ppst=co.prepareStatement(sql);
       rs=ppst.executeQuery();
       if(rs.next()){

       String add0 = rs.getString("StudentoNr");
       jTextField1.setText(add0);
       String add1 = rs.getString("Vardas");
       jTextFieldVardas.setText(add1);
       String add2 = rs.getString("Pavardė");
       jTextFieldPavarde.setText(add2);
       String add3 = rs.getString("EPaštas");
       jTextFieldPastas.setText(add3);
       }

       }catch(Exception e){

           JOptionPane.showMessageDialog(null, e);

       }`

Answers


arcy February 2016

Well, if prepareStatement(sql) is unable to produce a prepared statement, then it might easily have returned null; that's easy to check with a breakpoint, and maybe with looking for a stacktrace. You have no space between "student" and "as" in the sql posted, that looks like a potential problem...

Post Status

Asked in February 2016
Viewed 3,993 times
Voted 14
Answered 1 times

Search




Leave an answer