0
 String[] header = {"Name", "Age", "Place", "Date of Birth","Qualification"};
        table_model = new DefaultTableModel(header, 3);
        table = new JTable(table_model);
        JScrollPane sp = new JScrollPane(table);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        table.setFillsViewportHeight(true); 

        JButton submit = new JButton("Submit");
        jp1.add(new JScrollPane(table));
        jp1.add(submit, BorderLayout.WEST);

        submit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {    
                try {

                    TableCellEditor editor=table.getCellEditor();
                    editor.stopCellEditing();

                    Connection con = DriverManager.getConnection(host, uName, uPass);
                    stmt = con.createStatement(); 

                    for(int i=0;i<table.getRowCount();i++){

                      String name = table.getValueAt(i,0).toString();
                      int age = Integer.parseInt(table.getValueAt(i,1).toString()); 
                      String place=table.getValueAt(i,2).toString();   
                      String dob=table.getValueAt(i,3).toString();
                      String qualification=table.getValueAt(i,4).toString();    
                      stmt.execute("insert into PERSONAL(NAME,AGE,PLACE,DOB,QUALIFCATION)values('"+name+"',"+age+",'"+place+"',"+dob+",'"+qualification+"')");  
                    }   
                }   
                catch (SQLException ex) {   
                    ex.printStackTrace();
                }
            }
        });

i created a table with three rows and four columns.if i enter all values in three rows and column it will be successfully submitted.if any empty value enter in any row or column or user leaves all rows and column empty it is showing null pointer exception.But how to handle this.if user enter any null value or leaves all cells empty.it want to display a message please enter some value.after filling all values in three rows only it will allow.how to achieve thhis

dic19
  • 17,821
  • 6
  • 40
  • 69
REDDY
  • 11
  • 5

1 Answers1

2

Your table.getValueAt(i, xxxx) may return a null so calling a toString() to a null throws a runtime NullPointerException.

I suggest having a if statement to check if the value at row, column is not null, then retrieve it's toString() value.

Just guessing where the code might throw a NullPointerException.

Please debug your application to see which line of code throws the NullPointerException and fix it accordingly.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228