代码之家  ›  专栏  ›  技术社区  ›  Venkat

是什么导致这段代码中的“NullPointerException”读取Excel单元格?

  •  1
  • Venkat  · 技术社区  · 14 年前

    我在用 用于阅读Excel。我被要求将这些单元格存储到MySQL5.1中。当我读Excel单元格时,我被扔了 NullPointerException . 这是我的代码片段:

      int rows = sheet.getPhysicalNumberOfRows();
    
      for(int r = 1; r<rows; r++) {
                HSSFRow row = sheet.getRow(r);
                int cols = row.getLastCellNum();
    
                for(int c=0; c < cols; c++) {
                    HSSFCell cell = row.getCell(c, Row.CREATE_NULL_AS_BLANK);
    
                    switch(cell.getColumnIndex())    {
    
                       case 0:
                           ....
                           ....
    
                       case 1:
                           ....
                           ....
    
    
                       case ...:
                           ....
                           ....
    
                    }
                }
      }
    
      // Here comes the code to insert into DB
    

    如果有人能确定它发生在哪里,尽快发布。如果我留下了一些逻辑你只要问我。。。

    4 回复  |  直到 14 年前
        1
  •  3
  •   Syntax    14 年前

    您的数组偏移量是否可能偏移了1(对于行循环)?

    for(int r = 1; r<rows; r++) {
      HSSFRow row = sheet.getRow(r);
    

    不是从偏移量1开始,而是从0开始。这更有意义(并且与列循环相匹配)。

    编辑#1:

      if (sheet == null) {
            return;
      }
    
      int rows = sheet.getPhysicalNumberOfRows();
      for(int r = 1; r<rows; r++) {
            HSSFRow row = sheet.getRow(r);
            if (row != null) {
                 int cols = row.getLastCellNum();
    
                 for(int c=0; c < cols; c++) {
                     HSSFCell cell = row.getCell(c, Row.CREATE_NULL_AS_BLANK);
    
                     if (cell != null) {
                        switch(cell.getColumnIndex())    {
                             case 0:
                             case 1:
                             case ...:
    
                         }
                     }
                 }
          }
     }
    

    如果你看 the JavaDoc 这很重要

    public int getPhysicalNumberOfRows()  {
      return _rows.size();
    }
    

    返回物理定义的行数( 不是 表中的行数)

    编辑#2: 为了进一步调试,请在For循环周围放置以下try/catch。

    try {
         // Your for loop code here
    } catch (Exception x) {
         x.printStackTrace();
    }
    

        2
  •  1
  •   sly7_7    14 年前

    我不解释你的NPE,但建议你用另一种方法来迭代工作簿,你试过这个吗?

    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        final Sheet sheet = workbook.getSheetAt(i);
        final Iterator<Row> rows = sheet.rowIterator();
        if (rows.hasNext()) {
           final Row header = rows.next();
           ....
        }
    }
    
        3
  •  0
  •   Andrew Bezzub    14 年前

    我猜单元格可能是空的,但您应该使用调试器来找出异常位置。

        4
  •  0
  •   paxdiablo    14 年前

    从给定的代码中 sheet , row cell 将为空。异常堆栈跟踪本身应该确切地告诉您是哪一行导致了问题。

    第一个想法是你得到了表中的物理行数,但是 getRow() 给你一个合乎逻辑的行。你可以考虑使用 getFirstRowNum() getLastRowNum() 似乎又回来了 排。

    但那只是我的想法。在我确定之前,还需要更多的信息。


    在给定异常的情况下获取堆栈跟踪 npe

    npe.printStackTrace();
    

    或:

    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw, true);
    npe.printStackTrace(pw);
    pw.flush();
    sw.flush();
    // sw.toString(); to get a string for putting into a text box or something else.