I am getting the error "Exception in thread "main" java.lang.NullPointerException"



  • package Pack1;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.usermodel.WorkbookFactory;
    
    class Demo
    {
        public static void main(String[] args) throws Throwable   
        {
            File f= new File("C:\\Workspace\\Sample\\Testdata\\tdata.xlsx");
            FileInputStream fis=new FileInputStream(f);
            Workbook wb=WorkbookFactory.create(fis);
            Sheet st =wb.getSheet("Sheet1");
            Row r=st.getRow(0);
            Cell c=r.getCell(0);
    
            String data =c.toString();
            System.out.println(data);
    
    
        }
    }
    


  • Checked with the same code on a different file. The code worked without issues.

    NullPointerException points to the fact that you have accessed null data on any of the above lines before.

    For line 23, if you are getting the NullPointerException while accessing cell, please make sure: - The sheet name is correct and it is present - The row and cell you are accessing is not empty

    If the issue is still not resolved, try with another file. That might help you debug the issue yourself.


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2