Hi, I have something wrong at table_academicMouseClicked() method which results me with a java.lang.NullPointerException error. basically trying to compare the surname in Jtable and look it up on the database to select the corresponing record so that I can set the label to the value from database. So when a user click on a a row from the JTable, it searches for the appropriate row in database in order to obtain all the values from there this is my objective. but it is not returning anything when I called rs.getString(....) what have I done wrong. Everything works just something wrong at table_academicMouseClicked()

public class MainMenu extends javax.swing.JFrame {


    Academic ac = new Academic();
    Connection conn = JavaConnect.ConnectDB();
    PreparedStatement pst = null;
    String sql; 
    ResultSet rs = null;







    /**
     * Creates new form MainMenu
     */
    public MainMenu() {
        initComponents();


    }      

    public void Update_table() {
        try {
        String sqlStatement = "SELECT Title, Forename, Surname, Role FROM AcademicInfo ";
        pst = conn.prepareStatement(sqlStatement);
        rs = pst.executeQuery();
        table_academic.setModel(DbUtils.resultSetToTableModel(rs));
        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
        finally {
             try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
                if (conn != null) {
                    conn.close();
                }

            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(MainMenu.class.getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }

        }
    }//end method 



    public void myProfile() {
      ac.retrieveAcademic();
        nameLabel.setText(""+ac.getTitle()+" "+ac.getForename()+" "+ac.getSurname());
        roleLabel.setText("Role:    " + ac.getRole());
        roomLabel.setText("Room:    " + ac.getRoom());
        pageLabel.setText("Page:    " + ac.getPage());
        hoursLabel.setText("Hours:   " + ac.getHours());
        phoneLabel.setText("Phone:   " + ac.getPhone());
        mobileLabel.setText("Mobile:  " + ac.getMobile());
        emailLabel.setText("Email:   " + ac.getEmail());
        imageLabel.setIcon(ac.getImage());

    }

      public void editProfile() {
        ac.retrieveAcademic();
       txt_id.setText(String.valueOf(ac.getAcademicId()));
       txt_title.setSelectedItem(ac.getTitle());
        txt_fn.setText(ac.getForename());
        txt_ln.setText(ac.getSurname());
       combo_role.setSelectedItem(ac.getRole());
        txt_room.setText(ac.getRoom());
        txt_page.setText(ac.getPage());
        txt_hours.setText(ac.getHours());
        txt_phone.setText(ac.getPhone());
        txt_mobile.setText(ac.getMobile());
        txt_email.setText(ac.getEmail());
    }


 private void myProfileTabStateChanged(javax.swing.event.ChangeEvent evt) {
       JTabbedPane sourceTabbedPane = (JTabbedPane) evt.getSource();
        int index = sourceTabbedPane.getSelectedIndex();
        if (index == 0) {
            myProfile();

        }
        else if (index == 1) {

           editProfile(); 
        }

        else if (index == 2) {
            Update_table(); 
        }



    }

    private void jPanel3ComponentShown(java.awt.event.ComponentEvent evt) {

    }

    private void but_saveActionPerformed(java.awt.event.ActionEvent evt) {
      try {
      int id = Integer.parseInt(txt_id.getText());    
      String title = txt_title.getSelectedItem().toString();
      String fname = txt_fn.getText();
      String sname = txt_ln.getText();
      String role = combo_role.getSelectedItem().toString();;
      String room = txt_room.getText();
      String page = txt_page.getText(); 
      String hours = txt_hours.getText();
      String phone = txt_phone.getText();
      String mobile = txt_mobile.getText(); 
      String email = txt_email.getText();
      String pw = txt_cp.getText();

     if (pw.equals("")) {
     sql = "UPDATE AcademicInfo SET academicid='"+id+"', Title='"+title+"',  Forename='"+fname+"', Surname='"+sname+"', Role='"+role+"', Room='"+room+"', Page='"+page+"', Hours='"+hours+"'  , Phone='"+phone+"'  , Mobile='"+mobile+"'  , Email='"+email+"'  where academicid='"+id+"' ";
     }
     else {

     sql = "UPDATE AcademicInfo SET academicid='"+id+"', Title='"+title+"',  Forename='"+fname+"', Surname='"+sname+"', Role='"+role+"', Room='"+room+"', Page='"+page+"', Hours='"+hours+"'  , Phone='"+phone+"'  , Mobile='"+mobile+"'  , Email='"+email+"' , Password='"+pw+"' where academicid='"+id+"' ";
     } 
      pst = conn.prepareStatement(sql); 
      pst.execute(); 
      JOptionPane.showMessageDialog(null, "Your details have been saved");



      } catch (Exception e) {
          JOptionPane.showMessageDialog(null, e);   }  
        finally {

            try {

                if (pst != null) {
                    pst.close();
                }
                if (conn != null) {
                    conn.close();
                }

            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(MainMenu.class.getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }
        }


    }

    private void table_academicMouseClicked(java.awt.event.MouseEvent evt) {
        try {
        int row = table_academic.getSelectedRow();
        String table_click = (table_academic.getModel().getValueAt(row, 2).toString());
        String sqlSt = "SELECT * FROM AcademicInfo WHERE Surname='"+table_click+"'";
        pst = conn.prepareStatement(sqlSt);
        rs = pst.executeQuery();

        if(rs.next()) {
            label_name.setText(""+rs.getString("Surname"));

        }
        }


        catch(Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }

Line 5 Connection conn = JavaConnect.ConnectDB(); I don't know how your JavaConnect class looks like. Also, it is odd to see a method name starts with an "uppercase" letter. The reason is that the method name does NOT follow the naming convention...

Line 165, when you deal with SQL as in your case, you should not directly compare string with string using "=" symbol. You should use WHERE Surname like '"+table_click+"';". Also, the table name and field name are case sensitive. Not sure if the field in your table is really Surname or surname.

Line 169, what does next() method do with your rs? Doesn't look right to me. Normally next() method shouldn't be called unless you know for sure that the data exists. Also, once next() is called, it returns the object in it and move on to the next one in the iterator.

Line 170, if the surname you are looking for does not exist, wouldn't the rs be null?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.