Null Pointer Exception in Apache poi

一曲冷凌霜 提交于 2019-12-12 02:14:29

问题


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

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

Thank you


回答1:


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++)

or for the width,

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

From the Javadoc for Dimension

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



来源:https://stackoverflow.com/questions/24604296/null-pointer-exception-in-apache-poi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!