使用JTable更新数据库时的NumberFormatException

时间:2014-11-29 09:23:27

标签: java swing jtable

我似乎无法用我的JTable中给出的值更新我的数据库。

我的代码:

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();
    }    
} 

一切正常,但当我点击GUI上的UPDATE按钮时,我收到错误:

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

1 个答案:

答案 0 :(得分:5)

显然,您尝试解析为int的其中一个字符串为null。

您应该检查jTable1.getValueAt(a,2)jTable1.getValueAt(a,3)jTable1.getValueAt(a,4)以确保它们不为空。