替换JTable Null值JAVA

时间:2013-09-25 20:10:42

标签: java null jtable

我有一个从mysql数据库加载的jTable,也可以输入数据。当这个数据被发布回数据库时,我需要检查Null值。我能够检查空值,但我在使用空值向单元格添加值时遇到问题。下面是我正在尝试的。任何帮助将不胜感激。

public void CheckGrid(){
        DefaultTableModel model = (DefaultTableModel)jTable2.getModel();

        int rows = model.getRowCount();

        for(int i=0; i<rows;i++){

            Object ob0 = model.getValueAt(i, 0); 
            Object ob1 = model.getValueAt(i, 1);
            Object ob2 = model.getValueAt(i, 2);
            Object ob3 = model.getValueAt(i, 3);
            Object ob4 = model.getValueAt(i, 4);
            Object ob5 = model.getValueAt(i, 5);
            Object ob6 = model.getValueAt(i, 6);
            Object ob7 = model.getValueAt(i, 7);
            Object ob8 = model.getValueAt(i, 8);
            Object ob9 = model.getValueAt(i, 9);
            Object ob10 = model.getValueAt(i, 10);

           if(ob0.toString().length()<1){
              model.setValueAt("NA", i, 0);
           }
           if(ob1.toString().length()<1){
              model.setValueAt("NA", i, 1);
           }
           if(ob2.toString().length()<1){
              model.setValueAt("NA", i, 2);
           }
           if(ob3.toString().length()<1){
              model.setValueAt("NA", i, 3);
           }
           if(ob4.toString().length()<1){
              model.setValueAt("NA", i, 4);
           }
           if(ob5.toString().length()<1){
              model.setValueAt("NA", i, 5);
           }
           if(ob6.toString().length()<1){
              model.setValueAt("NA", i, 6);
           }
           if(ob7.toString().length()<1){
              model.setValueAt("NA", i, 7);
           }
           if(ob8.toString().length()<1){
              model.setValueAt("NA", i, 8);
           }
           if(ob9.toString().length()<1){
              model.setValueAt("NA", i, 9);
           }
           if(ob10.toString().length()<1){
              model.setValueAt("NA", i, 10);
           }
        }
     }

也尝试过:

 if(ob10 != null && !ob10.equals("") && ob10.length() !=0){

这是我的记录器错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at SuperPackage.CreateRetailOrder.CheckGrid(CreateRetailOrder.java:737)
    at SuperPackage.CreateRetailOrder.jButton4ActionPerformed(CreateRetailOrder.java:838)
    at SuperPackage.CreateRetailOrder.access$900(CreateRetailOrder.java:15)
    at SuperPackage.CreateRetailOrder$11.actionPerformed(CreateRetailOrder.java:590)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:702)
    at java.awt.EventQueue$4.run(EventQueue.java:700)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

1 个答案:

答案 0 :(得分:3)

替换所有条件,例如:

if( ob5.toString().length()<1 )

使用:

if( ob5==null || ob5.toString().isEmpty() )

对于所有[obj0..obj10]

此外,您的所有代码都可能有点重构:

    public void CheckGrid(){
        DefaultTableModel model = (DefaultTableModel)jTable2.getModel();
        int rows = model.getRowCount();

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j <= 10; j++) {
                Object ob = model.getValueAt(i, j);
                if (ob == null || ob.toString().isEmpty()) {
                    model.setValueAt("NA", i, j);
                }
            }
        }
     }