// JTable table;
    FileDialog fd=new FileDialog(this,"选择保存文件的地址",
                                FileDialog.SAVE);
     fd.setFile("这里修改文件名.xls");
      fd.show();
   String strFile=fd.getDirectory()+fd.getFile();   FileOutputStream fos = new FileOutputStream(strFile);           HSSFWorkbook wb=new HSSFWorkbook();
           HSSFSheet hs=wb.createSheet();
           TableModel tm=jTable1.getModel();
           int row=tm.getRowCount();
           int cloumn=tm.getColumnCount();
           HSSFCellStyle style=wb.createCellStyle();
           style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
           style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
           style.setBorderRight(HSSFCellStyle.BORDER_THIN);
           style.setBorderTop(HSSFCellStyle.BORDER_THIN);
           style.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
           style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
           HSSFFont font = wb.createFont();
           font.setFontHeightInPoints((short)11);
           style.setFont(font);
           HSSFCellStyle style1=wb.createCellStyle();
           style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
           style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
           style1.setBorderRight(HSSFCellStyle.BORDER_THIN);
           style1.setBorderTop(HSSFCellStyle.BORDER_THIN);
           style1.setFillForegroundColor(HSSFColor.ORANGE.index);
           style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
           HSSFFont font1 = wb.createFont();
           font1.setFontHeightInPoints((short)15);
           font1.setBoldweight((short)700);
           style1.setFont(font);           for(int i=0;i<row+1;i++)
           {
                HSSFRow hr=hs.createRow(i);
                for(int j=0;j<cloumn;j++)
                {
                     if(i==0)
                     {
                          String value=tm.getColumnName(j);
                          int len=value.length();
                          hs.setColumnWidth((short)j,(short)(len*400));
                          HSSFRichTextString srts=new HSSFRichTextString(value);
                          HSSFCell hc=hr.createCell((short)j);
          //                hc.setEncoding((short)1);
                          hc.setCellStyle(style1);
                          hc.setCellValue(srts);
                     }
                     else
                     {
                          String value=tm.getValueAt(i-1,j).toString();
                          HSSFRichTextString srts=new HSSFRichTextString(value);
                          HSSFCell hc=hr.createCell((short)j);
     //                     hc.setEncoding((short)1);
                          hc.setCellStyle(style);                          if(value.equals("")||value==null)
                          {
                                hc.setCellValue(new HSSFRichTextString(""));
                          }
                          else
                          {
                                hc.setCellValue(srts);
                          }
                      }
                   }
               }
String value=tm.getValueAt(i-1,j).toString();  提示java.lang.NullPointerException
at com.fox_ice.TestDB.Main.jMenuItem17_actionPerformed(Main.java:1163)
at com.fox_ice.TestDB.Main$23.actionPerformed(Main.java:416)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

解决方案 »

  1.   

    String value=tm.getValueAt(i-1,j).toString();这一行出的错吗?
    如果真的是这样,你的代码能够走进这一句,说明tm不为空。那么可能是你的getValueAt这个函数在某种情形下返回了null,而且可能性非常大哦~~所以,你还是察看一下你的TableModel对象中是如何改写这个方法的,处理上是否有问题。
    个人意见,仅供参考。
      

  2.   

    tm.getValueAt(i-1,j) 返回null了,有格子是null值
      

  3.   

    String value=tm.getValueAt(i-1,j)==null?null:tm.getValueAt(i-1,j).toString();
      

  4.   

    原来用的是sql server  的没有问题 现在老板然用access  就不可以了 
    咋搞啊啊
      

  5.   

    String value=tm.getValueAt(i-1,j)==null?null:tm.getValueAt(i-1,j).toString();
    是对的