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);
 }
}
Alok
76011 silver badges25 bronze badges
asked Sep 29, 2018 at 16:37
3
  • 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. Commented Sep 29, 2018 at 17:22
  • Exception in thread "main" java.lang.NullPointerException at Pack1.Demo.main(Demo.java:23) Cell c=r.getCell(0); Commented Sep 29, 2018 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. Commented Oct 1, 2018 at 7:24

1 Answer 1

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.

answered Sep 30, 2018 at 21:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.