K
K
Kirill Savinov2018-10-21 12:14:16
Java
Kirill Savinov, 2018-10-21 12:14:16

What will happen in this case?

I have a method that reads a cell in an XLSX file, determines the type of the cell, and returns a cell of the specified type in String format. There is also a try-catch block that handles a NullPointerException that fires when I read a cell that doesn't exist (I don't know why it's such an exception, but it's written that way in the XLSX documentation), but that's not the point.
I'm wondering in which case return "OK" will work if I have all types of cells and even errors are processed, and in no case should return "OK" work, although the IDE requires me to put this return.
It is not necessary to go into the details of this example.

public String readByRowCell(int row, int cell) {
        try {
            CellType i = sheet.getRow(row).getCell(cell).getCellTypeEnum();
            switch (i) {
                case BLANK:
                    return "пустая ячейка";
                case _NONE:
                    return "Неизвестный тип";
                case STRING:
                    return sheet.getRow(row).getCell(cell).getStringCellValue();
                case NUMERIC:
                    return ((Double) sheet.getRow(row).getCell(cell).getNumericCellValue()).toString();
                case FORMULA:
                    return sheet.getRow(row).getCell(cell).getCellFormula();
                case ERROR:
                    return "Ошибочный тип ячейки";
                case BOOLEAN:
                    return sheet.getRow(row).getCell(cell).getCellFormula();
            }
        } catch (NullPointerException e) {
            return "Ошибка: данная ячейка не существует";
        }
        return "ОК"; // Когда это произойдёт?
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Cheypnow, 2018-10-21
@HaNij

When none of the cases are executed

A
arkuzo, 2018-10-21
@arkuzo

The compiler requires this return because it doesn't have an algorithm to check if all cell types are processed. If the program does not encounter a type not listed here, then the 'OK' string will not be returned.
Alternatively, after all the options, you can write the ```default:``` keyword, in the code block of which include return. Then the compiler will not need to return at the end of the function, because it will see that this is an unreachable place.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question