2

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

6
  • from your code, line 28 is String b1Val = cellB1.getStringCellValue(); Commented Mar 31, 2012 at 6:10
  • yeh that's why i added the line 28 on my code here as HSSFRow row1 = worksheet.getRow(0); because there are more comments before class and after code that are added by netbeans. So line 28 here not same as there, hence the inclusion of error line
    – sys_debug
    Commented Mar 31, 2012 at 6:13
  • 2
    so in your excel book you have a sheet with the name "POI Worksheet" (exact name)? Commented Mar 31, 2012 at 6:14
  • oh damn thats the error! I made sure the file name is correct but not the sheet! I hate excel! thanks @LuiggiMendoza for pointing out the problem. Thanks. You might want to post this as answer so it be marked.
    – sys_debug
    Commented Mar 31, 2012 at 6:16
  • @LuiggiMendoza I am making it more dynamic now...any idea how to get worksheet title automatically? because I am passing file path from JFileChooser but then I need to also specify sheet title.
    – sys_debug
    Commented Apr 1, 2012 at 5:54

1 Answer 1

3

Check that your excel book must have a sheet with the name "POI Worksheet" (exact name).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.