getSheet和getSheetAt引发Null指针异常

时间:2018-07-23 18:59:01

标签: java nullpointerexception apache-poi

我在加载Excel文件并在其中保存新信息时遇到了麻烦:

public void llenarTemplate(String sht, ResultSet rs){

    try {
        int row;
        row = 1;
        wb = new XSSFWorkbook(template);
        System.out.println(wb.getNumberOfSheets());
        for (int sn=0; sn < wb.getNumberOfSheets(); sn++) {
         System.out.println("Sheet " + sn + " is called " + wb.getSheetName(sn));
        }
      ws = wb.getSheet(sht);

      while(rs.next()){
         row++;
           cell = ws.getRow(row).getCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK);
           cell.setCellValue(rs.getString(1));     
           cell = ws.getRow(row).getCell(2, MissingCellPolicy.CREATE_NULL_AS_BLANK);
           cell.setCellValue(rs.getString(2));
       }       
    } catch (IOException ex) {
        Logger.getLogger(exportExcel.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(exportExcel.class.getName()).log(Level.SEVERE, null, ex);
    } 
}

当我尝试选择不为“ 0”的任何其他工作表时,问题就来了。即使我尝试按名称打开工作表0。每当我尝试在其上书写时,都会得到以下信息:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at FileGenerators.exportExcel.llenarTemplate(exportExcel.java:65)
at Menus.Update_Filters.jMenuItem7ActionPerformed(Update_Filters.java:1019)
at Menus.Update_Filters.access$2100(Update_Filters.java:25)
at Menus.Update_Filters$28.actionPerformed(Update_Filters.java:761)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我试图查看是否出了点问题,所以我写了这个:

System.out.println(wb.getNumberOfSheets());
    for (int sn=0; sn < wb.getNumberOfSheets(); sn++) {
     System.out.println("Sheet " + sn + " is called " + wb.getSheetName(sn));
    }

将其显示为结果

    Sheet 0 is called CATEGORÍAS
    Sheet 1 is called OBJETIVOS
    Sheet 2 is called TIPO DE CURSO
    Sheet 3 is called CÓDIGOS INSTITUCIONALES
    Sheet 4 is called STPS

无论如何,无论我是用getSheet(“ OBJETIVOS”)还是getSheetAt(1)选择它,我尝试在其中写入的第二秒都会收到错误。即使我调用getSheet(“CATEGORÍAS”),也会收到错误消息。

现在有人在做什么错吗?

提前谢谢!

0 个答案:

没有答案