0

I am using apache poi to extract data from excel sheet and trying to save extracted values in an array.

I am getting java.lang.NullPointerException error.

I searched about it, and apparently this exception triggered when you try to assign a value to an object whose value is null.

I tried to look up many example, but most example codes just print out cell value. I want to save them in an array to perform some calculation.

package exceltotext;
import java.io.*;
import java.lang.*;
import java.util.*;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class convert {

    @SuppressWarnings("null")
    public static void main(String[] args) throws IOException {
        double []  modify = null ;
        FileInputStream fs = new FileInputStream(new File ("Book1.xlsx"));
        XSSFWorkbook wb = new XSSFWorkbook(fs);
        XSSFSheet sheet = wb.getSheetAt(0);
        FormulaEvaluator formulaevaluater = wb.getCreationHelper().createFormulaEvaluator();
        for (Row row:sheet)
        {
            int  num = 0;
            for(Cell  cell : row){

                switch (formulaevaluater.evaluate(cell).getCellType())
                {
                case Cell.CELL_TYPE_NUMERIC:

                modify [num] = ( cell.getNumericCellValue());
                num += num;
                }
                System.out.println(modify[num]);
           }
        }
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
shujaat
  • 19
  • 5
  • 1
    Please, add stacktrace with NullPointerException to question. – Aleksandr Podkutin Aug 01 '16 at 14:13
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – CloudPotato Aug 01 '16 at 14:13
  • 1
    @shujaat btw `@SuppressWarnings("null")` will not save you here beace NullPointerException is a runtime exception and not a compiler warning. – tirpitz.verus Aug 01 '16 at 14:17

2 Answers2

1

Your modify array is null so num += num; will give you NPE. You need to create this array with for an egzample double [] modify = new double[1000]; instead of double [] modify = null ;

  • double [] modify = new double[1000]; creates a object of class double right?. but still we haven't assigned it to anything so it is basically null – shujaat Aug 01 '16 at 15:11
  • Also can we define it without giving it a limit like 1000 – shujaat Aug 01 '16 at 15:15
  • @shujaat it does not create an object but an array of type double. When you create an array it has a fixed length. If You would like to have a dynamic length than please use a collection (eg. `ArrayList modify = new ArrayList<>();` and than `modify.add(cell.getNumericCellValue());`) – tirpitz.verus Aug 02 '16 at 11:21
0

I think, first you need to define the rows, from where you are trying to extract the values.

Apache Poi have option for row i.e XSSFRow.

PS i would personally recommend HSSF instead of XSSF , hence HSSF is for new version of excel. You need to change the file extension name to use in HSSF if the same document is being used by XSSF (Cause of Version Incompatibility).

K. Dumre
  • 17
  • 8
  • I was using hssf but eclipse gave an error and recommended to use xssf(because file extension wa xlsx) – shujaat Aug 01 '16 at 15:18