0

mysql データベースからロードされた jTable があり、データも入力できます。このデータがデータベースに公開されたら、Null 値を確認する必要があります。null 値をチェックすることはできますが、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)
4

1 に答える 1

4

次のようなすべての条件を置き換えます。

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);
                }
            }
        }
     }
于 2013-09-25T22:24:58.650 に答える