Phillip Oosthuizen
Phillip Oosthuizen

Reputation: 29

I get the error that I must add return statement although I have added the return statement

public static ArrayList<Double> readSheet(XSSFSheet spreadsheet) {
    int rows = spreadsheet.getPhysicalNumberOfRows();
        
    for (int i = 0; i <= i; i++) {
        XSSFRow row = spreadsheet.getRow(i);
        
        if (row == null) {
            continue;
        }
        
        int cells = row.getPhysicalNumberOfCells();
        ArrayList<Double> cellList = new ArrayList<>();
    
        for (int j = 0; j < cells; j++) { //遍历每一列
            XSSFCell cell = row.getCell(j);
            cell.setCellType(cell.CELL_TYPE_STRING);
                
            //new Double("1.0").intValue()
            Double cellValue = cell.getNumericCellValue();
            cellList.add(cellValue);        
        }
            
        return cellList;
    }
}

Can someone please help

Upvotes: 1

Views: 50

Answers (2)

Jakub Biały
Jakub Biały

Reputation: 422

Firstly, your condition in for loop is wrong: i <= i is always true.
Secondly, you must provide return statement for all branches. For example, if condition in for loop will be false, there is no return statements in remaining function body.

You should:

  • put cellList before for loop
  • put return statement outside for loop

Upvotes: 0

jmizv
jmizv

Reputation: 1312

You should provide the return statement outside of your for loop:

public static ArrayList<Double> readSheet(XSSFSheet spreadsheet) {
    int rows = spreadsheet.getPhysicalNumberOfRows();
    
    ArrayList<Double> cellList = new ArrayList<>();
    
    for (int i = 0; i <= i; i++) {
        ...
    }
    return cellList;
}

Because the compile cannot decide if there is any run of your for loop and therefore not guarantee that your return statement will be reached.

This also means that you need to declare your cellList variable outside of the for loop.

And please also check your loop condition. While i<=i might be always true you don't want to loop indefinitely.

Upvotes: 2

Related Questions