0

My code reads data from an Excel sheet. But, after reading the filled cells, it enters another loop and read empty cells which makes my test fails, any advice?

public class readSheet {

    public Object[][] readSheet_Info() throws InvalidFormatException, IOException{
        File signUp = new File(".\\Test-Data\\Datasheet.xlsx");
        XSSFWorkbook signUpsheet = new XSSFWorkbook (signUp);
        XSSFSheet sheet = signUpsheet.getSheet("sheet1");
        int rows = sheet.getPhysicalNumberOfRows();
        int columns = sheet.getRow(0).getLastCellNum();
        String[][] sheetarray = new String [rows-1][columns];

        
        for(int i=1; i<rows; i++) {
            
            for(int j=0; j<columns; j++) {
                XSSFRow row = sheet.getRow(i);
                sheetarray[i-1][j]= String.valueOf(row.getCell(j));
                    
                    
                
            }
        }
        
        return sheetarray;
    }
}
marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
  • 1
    The loop starts twice? you would still be reading the same cells in that case. I suggest you go through your code with a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Alias Cartellano Jan 13 '22 at 17:53
  • Stick to acceptable naming conventions. In Java, the naming convention for class name is to use "Camel Case". That means, the first letter of the first word should also be capitalized. – hfontanez Jan 13 '22 at 18:51
  • 1
    `XSSFRow row = sheet.getRow(i);` should appear after the for-i loop but before the for-j loop - row can be null, so you should check if row==null – PJ Fanning Jan 13 '22 at 21:34

0 Answers0