简体   繁体   中英

NumberFormatException while updating database with JTable

I cannot seem to update my database with values given in my JTable.

My code :

public void Update() throws SQLException 
{
    for (int a = 0; a < jTable1.getRowCount(); a++) {
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//127.0.0.1:1521/xe","IND","pass");
        Statement stmt = con.createStatement();
        String query4 = "insert into MONTH_inv VALUES (?,?,?,?,?)";
        String Category = jTable1.getValueAt(a, 0).toString();  
        String Item = jTable1.getValueAt(a, 1).toString();
        int Quantity = Integer.parseInt((String) jTable1.getValueAt(a, 2));
        int avg = Integer.parseInt((String) jTable1.getValueAt(a, 3));
        int ground = Integer.parseInt((String) jTable1.getValueAt(a, 4));

        PreparedStatement pstmt = con.prepareStatement(query4);
        pstmt.setString(1, Category);
        pstmt.setString(2, Item);
        pstmt.setInt(3, Quantity);
        pstmt.setInt(4, avg);
        pstmt.setInt(5, ground);
        pstmt.executeUpdate();
    }    
} 

Everything is working fine, but when I click UPDATE button on my GUI, I get the error:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: null

Obviously, one of the Strings you try to parse as int is null.

You should check jTable1.getValueAt(a,2) , jTable1.getValueAt(a,3) and jTable1.getValueAt(a,4) to make sure they are not null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM