Java Cell.getNumericCellValue在读取Excel文件时返回空值

Java Cell.getNumericCellValue在读取Excel文件时返回空值,java,nullpointerexception,apache-poi,Java,Nullpointerexception,Apache Poi,下面是在循环中打印同一行中的单元格总和的代码。当我运行时,第一个循环工作正常,我得到了sum的值,但在第二个循环中,它在第double z=cell.getNumericCellValue()行抛出空指针异常在打印z的值后 try{ FileInputStream file = new FileInputStream("C:/Documents and Settings/m.y/My Documents/test.xls"); HSSFWorkbook workbook

下面是在循环中打印同一行中的单元格总和的代码。当我运行时,第一个循环工作正常,我得到了sum的值,但在第二个循环中,它在第
double z=cell.getNumericCellValue()行抛出空指针异常
在打印
z
的值后

try{
      FileInputStream file = new FileInputStream("C:/Documents and Settings/m.y/My Documents/test.xls");

      HSSFWorkbook workbook = new HSSFWorkbook(file);
      HSSFSheet sheet = workbook.getSheetAt(0);
 //   HSSFCell cell = null;
      Row r = sheet.getRow(0);
      Row row = sheet.createRow(0);
      Cell cell = row.createCell(0);
      double summ=1.0;

        for(int i =1 ; i<=sheet.getLastRowNum(); i++){

            for(int j=1; j<r.getLastCellNum();j++)
            {          
                cell = sheet.getRow(i).getCell(j); 
              //  if(HSSFCell.CELL_TYPE_NUMERIC==cell.getCellType())
             //   {  
                  double z= cell.getNumericCellValue(); 
                   System.out.println(z);                  
                   summ=summ+z;  

             //    }                                     
            }  
             System.out.println(summ);
             Cell mycell1=r.createCell(3);
                   mycell1.setCellValue(summ); 
                   summ=0.0;        
                 }    

    file.close();    
    FileOutputStream outFile =new FileOutputStream("C:/Documents and Settings/m.y/My Documents/test.xls");
    workbook.write(outFile);
    outFile.close();   


}catch (IOException e) {
    e.printStackTrace();
}
    }
试试这个

try{
        FileInputStream file = null;
        try {
            file = new FileInputStream("C:/Documents and Settings/m.y/My Documents/test.xls");
            HSSFWorkbook workbook = new HSSFWorkbook(file);
            HSSFSheet sheet = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();
            double summ = 1.0;
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();
                int i = 0;
                double z = 0.0;
                while (cellIterator.hasNext()) {
                    cell = sheet.getRow(i).getCell(j);
                    if (i == 0) {
                        System.out.println(cell.getStringCellValue());
                    } else {
                        z = cell.getNumericCellValue();
                    }
                    System.out.println(z);
                    summ = summ + z;
                    i++;
                    //    }                                     
                }
                System.out.println(summ);
                Cell mycell1 = r.createCell(3);
                mycell1.setCellValue(summ);
                summ = 0.0;
            }
            file.close();
            FileOutputStream outFile = new FileOutputStream("C:/Documents and Settings/m.y/My Documents/test.xls");
            workbook.write(outFile);
            outFile.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                file.close();
            } catch (IOException ex) {
                Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
试试看{
FileInputStream文件=null;
试一试{
file=newfileinputstream(“C:/Documents and Settings/m.y/My Documents/test.xls”);
HSSF工作簿=新的HSSF工作簿(文件);
HSSFSheet sheet=工作簿。getSheetAt(0);
迭代器rowIterator=sheet.Iterator();
双总和=1.0;
while(roweiterator.hasNext()){
行=行迭代器。下一步();
迭代器cellIterator=row.cellIterator();
int i=0;
双z=0.0;
while(cellIterator.hasNext()){
单元格=表.getRow(i).getCell(j);
如果(i==0){
System.out.println(cell.getStringCellValue());
}否则{
z=cell.getNumericCellValue();
}
系统输出打印ln(z);
sum=sum+z;
i++;
//    }                                     
}
系统输出打印项次(总和);
细胞菌丝1=r.createCell(3);
菌丝1.集细胞值(总和);
总和=0.0;
}
file.close();
FileOutputStream outFile=新的FileOutputStream(“C:/Documents and Settings/m.y/My Documents/test.xls”);
工作簿。写入(输出文件);
outFile.close();
}捕获(FileNotFoundException ex){
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE,null,ex);
}最后{
试一试{
file.close();
}捕获(IOEX异常){
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE,null,ex);
}
}

您可以发布程序的输出,并在抛出错误的行中添加注释吗?单元格可能为空,您应该检查它cell==null。首先它打印单元格的内容,即z(例如,对于上面的excel,2.0 3.0),然后求和5.0,在第二个循环之后,它打印单元格(5.0 6.0)但是没有求和,在reading.reading.main(reading.java:88)时在线程“main”java.lang.NullPointerException中抛出异常。我在数值数据之前有一些字符串单元格,使用迭代器,我得到这个异常,线程“main”中的异常java.lang.IllegalStateException:无法从文本cellmanage i变量中相应地获取数值,无论之前有什么单元格,以及希望捕获为数值的任何值,它仅在此行中给出空指针异常,z=cell.getNumericCellValue();
try{
        FileInputStream file = null;
        try {
            file = new FileInputStream("C:/Documents and Settings/m.y/My Documents/test.xls");
            HSSFWorkbook workbook = new HSSFWorkbook(file);
            HSSFSheet sheet = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();
            double summ = 1.0;
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();
                int i = 0;
                double z = 0.0;
                while (cellIterator.hasNext()) {
                    cell = sheet.getRow(i).getCell(j);
                    if (i == 0) {
                        System.out.println(cell.getStringCellValue());
                    } else {
                        z = cell.getNumericCellValue();
                    }
                    System.out.println(z);
                    summ = summ + z;
                    i++;
                    //    }                                     
                }
                System.out.println(summ);
                Cell mycell1 = r.createCell(3);
                mycell1.setCellValue(summ);
                summ = 0.0;
            }
            file.close();
            FileOutputStream outFile = new FileOutputStream("C:/Documents and Settings/m.y/My Documents/test.xls");
            workbook.write(outFile);
            outFile.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                file.close();
            } catch (IOException ex) {
                Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
            }
        }