-4

I have a sheet,it includes: number,name,pass,access,result. I want get name,pass,result to testweb, I try writing it but i get this error :

String user[],pass[],result[];
Workbook workbook = WorkbookFactory.create(new File(pathFile));
Sheet sheet = workbook.getSheetAt(0);
workbook.close();
if (sheet != null) {
int rowindex =0;
for(Row row : sheet) {
   for(Cell cell:row){
      int i=cell.getColumnIndex();
      if(i==1) user[rowindex]=cell.toString();
      if(i==2) pass[rowindex]=cell.toString();
      if(i==4) result[rowindex]=cell.toString();
      }
      rowindex++;
  }
} else {
    System.out.println("Sheet was not found");
  }    
Quang Dao Van
  • 17
  • 1
  • 5

2 Answers2

0

Not a lot of details in your question but based on the code you have I'm guessing your doing something like this

 FileInputStream inputStream = new FileInputStream(new File(file));
 Workbook workbook = new XSSFWorkbook(inputStream);
 Sheet sheet = workbook.getSheetAt(0);

My guess is your sheet is NULL. So you need to put some guard code in checking to make sure you have a valid sheet.

Something like this

 if (sheet != null) {
    int n = sheet.getLastRowNum();
    // Do the rest of you code
} else {
    System.out.println("Sheet was not found Check to make sure file was read and sheet was access properly.");
}          
Quang Dao Van
  • 17
  • 1
  • 5
Mongo
  • 31
  • 5
0

it worked

   if (sheet != null) {  
          rowNum=sheet.getLastRowNum()+1; //getlastrow() return index last row
          String username[]=new String[rowNum];
          int index=0;
          Iterator<Row> rowIterator = sheet.iterator();
                while (rowIterator.hasNext()) {
                        Row row = rowIterator.next();
                        Iterator <Cell> cellIterator = row.cellIterator();
                        index++;
                           while (cellIterator.hasNext()) {
                               Cell cell = cellIterator.next();
                               if(cell.getColumnIndex()==1) 
                               username[index]=cell.toString();
                            }
                   }

     } else {
        System.out.println(" Sheet was not found ");
     }
Quang Dao Van
  • 17
  • 1
  • 5