Null Pointer Exception in Apache poi

2019-09-19 05:58发布

问题:

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.