0

I want to sum up all values in the cells of the rows and write the aggregated value in the bottom of the table. I use this code

double Sum[] = new double[sheet.getRow(0).getLastCellNum()];
for (int i = 1; i < LastCellNum; i++)
{
    Sum[i - 1] = 0;

    for (int j = 1; j <= LastRowNum; j++)
    {
        cell = sheet.getRow(j).getCell(i);
        if (cell != null)
        {
            try
            {
                Sum[i - 1] = Sum[i - 1] + cell.getNumericCellValue();
            }
            catch (NumberFormatException | NullPointerException ex)
            {
                //Handle NumberFormat and NullPointer exceptions here
            }
            catch (Exception ex)
            {
                //Handle generic exception here}
            }
        }
    }
    System.out.println(Sum[i - 1]);

The code works properly and it could even print the Value of Sum, but after that I have java.lang.NullPointerException. All the selected Cells in my file are either empty or contain numbers.

Impulse The Fox
  • 2,638
  • 2
  • 27
  • 52
Ezz Eddin Othman
  • 163
  • 1
  • 12
  • And unrelated: read about java naming conventions: variable names go camelCase. "Sum" is pretty misleading therefore. And hint: you dont **catch** and handle NPEs. They are BUGS. You fix them. – GhostCat Nov 30 '18 at 13:05
  • Please provide information about the line the exception is in. You can use `ex.printStacktrace()` to get a full stack trace. – Adder Nov 30 '18 at 13:06
  • `j <= LastRowNum` – Joakim Danielson Nov 30 '18 at 14:07

0 Answers0