0
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);


    }
}
  • 2
    Welcome to StackExchage. There is no Selenium related code. So have removed the tag. On which line code are you getting the null pointer exception. – Alok Sep 29 '18 at 17:22
  • Exception in thread "main" java.lang.NullPointerException at Pack1.Demo.main(Demo.java:23) Cell c=r.getCell(0); – Radhika Sep 29 '18 at 18:34
  • That means the cell that you are trying to access has no values hence you a getting a null pointer exception. Make that cell has some value. – Alok Oct 1 '18 at 7:24
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.