-1

The problem I am facing arises whenever I am trying to obtain a cell. The funny part is that this function works fine till it reaches this part of the code.

        for (i = 1; i < 555; i++) 
        {           
            try
            {
                cell = sheet2.getRow(i).getCell(columnNo);              
            }
            catch (NullPointerException e)
            {               
                cell = sheet2.getRow(i).createCell(columnNo);
            }
            value = "NOT(ISERROR(MATCH(C" + (i + 1) + ",$N$2:$N$710,0)))";
            cell.setCellFormula(value);
        }

Because of try catch i get the error for createCell, but i believe that the problem lies at the line where i use getCell. I get the following error : Exception in thread "main" java.lang.NullPointerException

Just before this part, i have a for loop(pasted below) which works just fine.

        for (i = 1; i <= incorrect.size(); i++) {
            cell = sheet2.getRow(i).getCell(columnNo);
            if (cell == null)
                cell = sheet2.getRow(i).createCell(columnNo);
            value = incorrect.get(i - 1);
            cell.setCellValue(value);
        }
  • 3
    i guess your `sheet2` value is null. debug your code line by line and you will find a problem – Vault23 Oct 14 '19 at 09:24
  • You should provide us more code. What is sheet2? Do you have a sheet1? when do you increase columnNo? What is your example input? I don't want you to paste us 555 rows, but a minimal example of how do you get your exception. On which line do you get the exception? – aBnormaLz Oct 14 '19 at 09:26
  • you never called getCellValue in the shared code snippet – Vinay Prajapati Oct 14 '19 at 09:33
  • I don't think there is a problem getting the sheet. The cell which i am trying to set a formula for will store a boolean value. Not sure if that is a problem when retrieving the cell. – user11215077 Oct 14 '19 at 09:37

1 Answers1

0

Realized my error. The loop was set to run for 554 times. However the excel file had only 553 entries. Hence there was a problem.