0
votes

I am trying to fetching data from excel sheet(.xlsx) file. when I use to print fetched data using

System.out.println(sheet.getRow(i).getCell(c).getStringCellValue()); I see the all the rows from excel sheet is fetched. But when I add this data in jtable it misses last three rows and shows following error:

Cannot invoke "org.apache.poi.xssf.usermodel.XSSFRow.getCell(int)" because the return value of "org.apache.poi.xssf.usermodel.XSSFSheet.getRow(int)" is null

enter code here


     JFileChooser fileChooser = new JFileChooser("D:");
    
        int returnValue = fileChooser.showOpenDialog(null);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
        selectedFile = fileChooser.getSelectedFile();

        FileName = selectedFile.getName();
        String FilePath = selectedFile.getPath();
        System.out.println(FileName);
        System.out.println(FilePath);
      
        File excelfile = new File(FileName);
        
       try{ 
       
       FileInputStream fis = new FileInputStream(selectedFile);
         
       XSSFWorkbook wb = new XSSFWorkbook(fis);
       XSSFSheet sheet = wb.getSheetAt(0);
       int totalrows = sheet.getPhysicalNumberOfRows();
       
                
    
       for (int i = 0; i <=totalrows; )
       {
    
        dmodel.addRow(new Object[]{"" });
     
       System.out.println(sheet.getRow(i).getCell(0).getStringCellValue());   // this line work
       String name = sheet.getRow(i).getCell(0).getStringCellValue();  
       jTable1.setValueAt(name , i, 0);    // this line does not work        
        
       i++ ;
      
       
       
      } 
       
          JOptionPane.showMessageDialog(null, "All rows are fetched Successfully" );
       
       
       
   }catch(Exception fx)
          {
          System.out.println(fx.getMessage()); 
         // System.out.println(fx.getCause());
          
          }
1
However if i add three empty rows in excel sheet in the bottom of the sheet then it works fine.Furqan Ahmed Khan
Are you sure that the error is thrown at the line you indicated? It seems strange to me that you recive an ApachePoi-related error which is thrown at a line that has nothing to do with ApachePoi but only refears to the use of a JTable (mabye you should post the full error).VinceLomba

1 Answers

1
votes
   System.out.println(sheet.getRow(r).getCell(c).getStringCellValue());   // this line work
   String name = sheet.getRow(i).getCell(c).getStringCellValue();  
   jTable1.setValueAt(name , i, 0);    // this line does not work

Don't know if this will solve your problem, but the above code is not how you use System.out.println(...) to debug your logic.

To verify the data you use:

   //System.out.println(sheet.getRow(r).getCell(c).getStringCellValue());   // this line work
   String name = sheet.getRow(i).getCell(c).getStringCellValue();  
   System.out.println( name );
   jTable1.setValueAt(name , i, 0);    // this line does not work

Don't attempt to read the data twice. By assigning the data to a variable you are debugging the data in the variable. Don't repeat the nested methods multiple times.

Maybe the class expects you to only read the data once, so the second read gives the null value. The simple change above will prevent this.