Reputation: 4003
I am trying to read this file I created as sample made up from 4 columns and 1 row. The code below was taken to test the API i am using i.e. Apache POI..
package testjavaexcel;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
*
*/
public class TestJavaExcel {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
FileInputStream fileInputStream = new FileInputStream("poi-test.xls");
HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
HSSFSheet worksheet = workbook.getSheet("POI Worksheet");
HSSFRow row1 = worksheet.getRow(0);
HSSFCell cellA1 = row1.getCell((short) 0);
String a1Val = cellA1.getStringCellValue();
HSSFCell cellB1 = row1.getCell((short) 1);
String b1Val = cellB1.getStringCellValue();
HSSFCell cellC1 = row1.getCell((short) 2);
boolean c1Val = cellC1.getBooleanCellValue();
HSSFCell cellD1 = row1.getCell((short) 3);
Date d1Val = cellD1.getDateCellValue();
System.out.println("A1: " + a1Val);
System.out.println("B1: " + b1Val);
System.out.println("C1: " + c1Val);
System.out.println("D1: " + d1Val);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The error coming from line "HSSFRow row1 = worksheet.getRow(0);" output is:
Exception in thread "main" java.lang.NullPointerException at testjavaexcel.TestJavaExcel.main(TestJavaExcel.java:28) Java Result: 1
Not sure why this is happening...it seems like straight forward.note that the .getCell() methods invoked are all striked indicating deprecated methods but not sure how can I replace them given the API.
Thanks,
UPDATE: I figured out that the new method if getCell takes int instead of older version using short type. that fixed the deprecated warning. The rest remains unsolved. Also i am using poi version 3.8
Upvotes: 2
Views: 3261
Reputation: 85781
Check that your excel book must have a sheet with the name "POI Worksheet" (exact name).
Upvotes: 3