main.java 中的一段代码
 rsp=stmt.executeQuery("select p_id, p_name,p_type, p_out,p_danwei,p_begintime,p_shuliang p_price,p_nowvalue,p_shuliang from property where p_name!='' and p_shuliang>0");
 DefaultTableModel model = new DefaultTableModel();
 jTable1 = new JTable();
 jTable1.setModel(model);   ResultSetMetaData rsmd = rsp.getMetaData();
   int numberOfColumns=rsmd.getColumnCount();
   int b=1;
   //以下是显示数据库中的列名的代码
   while(b<=numberOfColumns) {
       model.addColumn(rsmd.getColumnName(b));
        b++;
   }
   //以下是在表格中显示数据中的内容   while(rsp.next()) {
       Vector newRow=new Vector();
       int c=1;
       while(c<=numberOfColumns){
           newRow.addElement(rsp.getString(c));
           c++;
       }
       model.addRow(newRow);
   }
     //   jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      //  RowSorter  <TableModel>  sorter = new  TableRowSorter (model);
    //    jTable1.setRowSorter(sorter);
       JScrollPane pane = new JScrollPane(jTable1);去调用
   Main main = new Main();   Vector rowdata = new Vector(100);
   rowdata.addElement(p_id);
   rowdata.addElement(p_name);
   rowdata.addElement(p_type);
   rowdata.addElement(p_out);
   rowdata.addElement(p_danwei);
   rowdata.addElement(p_begintime);
   rowdata.addElement(p_price);
   rowdata.addElement(p_nowvalue);
   rowdata.addElement(p_shuliang);
   
   
   main.model.addRow(rowdata);
 
   main.jTable1.setModel(main.model);
  SwingUtilities.updateComponentTreeUI(main.jTable1);
   rs.close(); rs3.close(); rs2.close(); rs4.close(); rs5.close(); rs6.close(); rs7.close();
    }
    catch(Exception se)
    {
      se.printStackTrace();
    }
java.lang.NullPointerException

解决方案 »

  1.   

     main.model.addRow(rowdata);
      是空指针异常
      

  2.   

    你看看是不是main.model为空,Debug一下。
      

  3.   

    谢谢 大哥啊 是对的 但是还是不能刷新啊 jtable
      

  4.   

    谁能解决刷新jtable  50全给了
      

  5.   

    main.model没初始化?
    比如 main.model = new DefaultTableModel();
    然后再使用
      

  6.   

    刷新JTable,就是刷新DefaultTableModel,你看看程序中的model是否有值,估计是没有值。
      

  7.   

    你在程序里面多加上几个  system.out.println();  看看 哪个没有打印出来  不就知道 卡在哪了   
      

  8.   

    main.java的一部分代码 jtable
     public void uishow()throws Exception
      {
        Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
        dbConn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433; databsaeName=pubs", "sa", "");
        Statement stmt=dbConn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
        rsp=stmt.executeQuery("select p_id, p_name,p_type, p_out,p_danwei,p_begintime,p_shuliang p_price,p_nowvalue,p_shuliang from property where p_name!='' and p_shuliang>0");
        ResultSetMetaData rsmd = rsp.getMetaData();
        int numberOfColumns=rsmd.getColumnCount();
        int b=1;
        model = new DefaultTableModel();
        jTable1 = new JTable();
        jTable1.setModel(model);    while(b<=numberOfColumns) {
            model.addColumn(rsmd.getColumnName(b));
             b++;
        }    while(rsp.next()) {
            Vector newRow=new Vector();
            int c=1;
            while(c<=numberOfColumns){
                newRow.addElement(rsp.getString(c));
                c++;
            }
            model.addRow(newRow);
        }
        addproperty add=new addproperty();
     //   add.dataChanged(int  p_id,String p_name,String p_type,String p_out, String p_danwei,String p_begintime,String p_price,String p_nowvalue,String p_shuliang);
            pane = new JScrollPane(jTable1);
           
    }[color=#FF0000]add.java[/color]当 当点添加一条记录的时候  Main main = new Main();
     Vector rowdata = new Vector(111);
     rowdata.addElement(p_id);
     rowdata.addElement(p_name);
     rowdata.addElement(p_type);
     rowdata.addElement(p_out);
     rowdata.addElement(p_danwei);
     rowdata.addElement(p_begintime);
     rowdata.addElement(p_price);
     rowdata.addElement(p_nowvalue);
     rowdata.addElement(p_shuliang);
     main.model.addRow(rowdata);
     main.jTable1.setModel(main.model);
     main.jTable1.revalidate();
     main.jTable1.repaint();
     main.jTable1.updateUI();
        JOptionPane.showMessageDialog(this,"添加成功","消息!",JOptionPane.INFORMATION_MESSAGE);
     两个文件不是同一个类 在两个java文件中
      

  9.   

    Main main = new Main();//这行不要,你重新创建了,应该获取前面的实例
     Vector rowdata = new Vector(111);
     rowdata.addElement(p_id);
     rowdata.addElement(p_name);
     rowdata.addElement(p_type);
     rowdata.addElement(p_out);
     rowdata.addElement(p_danwei);
     rowdata.addElement(p_begintime);
     rowdata.addElement(p_price);
     rowdata.addElement(p_nowvalue);
     rowdata.addElement(p_shuliang);
     main.model.addRow(rowdata);
     main.jTable1.setModel(main.model);
     main.jTable1.revalidate();
     main.jTable1.repaint();
     main.jTable1.updateUI();
     JOptionPane.showMessageDialog(this,"添加成功","消息!",JOptionPane.INFORMATION_MESSAGE);
    红字的都不要了。