Apache poi 中的空指针异常 [英] Null Pointer Exception in Apache poi

查看:40
本文介绍了Apache poi 中的空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FileInputStream input=new FileInputStream(new File("TestCase.xls"));
HSSFWorkbook workbook=new HSSFWorkbook(input);
HSSFSheet sheet=workbook.getSheet("KeywordFramework");
System.out.println("i am in"); 
int rowNum = sheet.getLastRowNum() + 1;
System.out.println(rowNum);
int colNum = sheet.getRow(0).getLastCellNum();
System.out.println(colNum);
String data [][] = new String[rowNum][colNum];
for(int i =1 ; i< rowNum;i++)       
{
    System.out.println("1");
    HSSFRow row = sheet.getRow(i);
    for(int j = 0; j< colNum;j++)           
    {
    System.out.println("2");
    HSSFCell cell = row.getCell(j);
    String Cellvalue = cellToString(cell);
    System.out.println("cellvalue === "+Cellvalue);
    switch(j)
    {
        case 0 : Function ; break;
        case 1 : Function ; break;
        case n : Function(has 2 nested for loops) ; break;
    } // for switch
    Function;
} // for j loop } // for i loop

发现空指针异常,我在 n 的情况下调用了一个单元格并停止运行,但仍然从 excel 中再次调用相同的值,并试图将该单元格值放入我的代码中,但无法这样做由于没有更多情况,我不希望我的代码在同一行中再次调用 excel 单元格,它应该跳出循环并执行未发生的下一行

Null pointer exception Found , i have called one of a cell in case n and braked out but still the same value is called again from the excel and is trying to place that cell value in my code and is not able to do so as there is no more cases , i dont want my code to call the excel cell again in the same row , it should jump out of loop and execute the next row which is not happening

谢谢

推荐答案

你的问题不清楚,如果你想遍历元素的高度

Your question isn't clear, if you want to loop over the height of the element that would be

for(int a=0;a<SOT.getHeight();a++)

或宽度,

for(int a=0;a<SOT.getWidth();a++)

来自 Dimension

Dimension 类将组件的宽度和高度(以整数精度)封装在单个对象中.

The Dimension class encapsulates the width and height of a component (in integer precision) in a single object.

这篇关于Apache poi 中的空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆