-2

I have two JFrame instances. Main frame has the code that populates the data from the DB to table and the other is the jFrame2 whereas the user adds or updates a record table from the Main Frame will automatically refresh

Here's my code for the Main Frame:

public void fillTable(){

String sql = "SELECT ID, UniqueCode, Name, Age, Gender, Cell FROM info";
try{

    pst = conn.prepareStatement(sql);
    rs = pst.executeQuery();
    jTable1.setModel(DbUtils.resultSetToTableModel(rs));

    jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    jTable1.getColumnModel().getColumn(0).setPreferredWidth(60);
    jTable1.getColumnModel().getColumn(1).setPreferredWidth(250);
    jTable1.getColumnModel().getColumn(2).setPreferredWidth(100);
    jTable1.getColumnModel().getColumn(3).setPreferredWidth(60);
    jTable1.getColumnModel().getColumn(4).setPreferredWidth(60);
    jTable1.getColumnModel().getColumn(5).setPreferredWidth(92);

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

And Here's my Code for frame 2

private void save(){
    try{
        if(name.getText().isEmpty() || age.getText().isEmpty() || cell.getText().isEmpty()){
            JOptionPane.showMessageDialog(null, "Empty Fields Are Not Allowed", "System Message", JOptionPane.WARNING_MESSAGE);
        }
        else if(" ".equals(cmbGender.getSelectedItem())){
            JOptionPane.showMessageDialog(null, "Empty Fields Are Not Allowed", "System Message", JOptionPane.WARNING_MESSAGE);
        }
        else{
            connect = mysqlconnect.DBConnect(); 
            stmt = connect.createStatement();
            rs = stmt.executeQuery("SELECT Name FROM info WHERE Name='" + name.getText() + "'");

            if(rs.first()){
              JOptionPane.showMessageDialog(null, "Name already Exist, Duplicate info is not allowed", "System Message", JOptionPane.WARNING_MESSAGE);

            }else{
              sql = "INSERT INTO info(UniqueCode, Name, Age, Gender, Cell) values ('" + uniqueID.getText()+ "', '" + name.getText() + "', '" + age.getText() + "', " +
                    " '" + cmbGender.getSelectedItem() + "', '" + cell.getText() + "') ";

              Statement st = connect.createStatement();
              st.executeUpdate(sql);

              JOptionPane.showMessageDialog(null, "Successfully Saved", "System        Message", JOptionPane.INFORMATION_MESSAGE);

             this.dispose();
             frame.jTable1.repaint(); <====== Error comes here because i want to              refresh my table
            } 
        }
    }
    catch(Exception ex){
        System.out.println(ex);
    }
JC Ilagan
  • 1
  • 1
  • 2
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson May 03 '13 at 13:28
  • 1
    Where is the NullPointerException thrown? You should provide more details. – Kai May 03 '13 at 13:29
  • 1
    Don't use multiple `JFrames`. Post the exception so that we can figure out what is wrong. – Amarnath May 03 '13 at 13:44
  • I second all the above, but we can work on the design of your GUI after you solve this problem. However if we're to be able to help you, you will want to give us enough information to be able to help. So most importantly is -- what line throws the NullPointerException (as per @user714965)? Also where do you try to update the JTable's model in your second bit of code? – Hovercraft Full Of Eels May 03 '13 at 15:20
  • @HovercraftFullOfEels my code for updating the table is frame.jTable1.repaint(); and that's where the error comes from... – JC Ilagan May 04 '13 at 07:13

1 Answers1

0

You state that this line causes your NPE:

frame.jTable1.repaint(); 

Then either frame or frame.jTable1 is null. Change the above line to:

System.out.println("frame == null: " + (frame == null)); 
System.out.println("frame.jTable1 == null: " + (frame.jTable1 == null));     
frame.jTable1.repaint(); 

Then after you find out which variable is null, trace back in your code to see where you didn't initialize it.

Note that you should never have to call repaint() on a JTable that this does not "refresh" data or do anything of the sort. Instead you need to change the data held by the JTable's model, something I don't see you doing in your posted code.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Can you please tell what's the code on how will my JTable will be updated – JC Ilagan May 06 '13 at 10:45
  • @JCIlagan: again, you would update or replace the JTable's model. How you do that depends on how your program is set up. The Swing JTable tutorials tell you how to use table models, so you might want to give them a study. – Hovercraft Full Of Eels May 06 '13 at 18:07
  • instead of repaint you can use fireTableDataChanged(); – Neo May 22 '13 at 17:30