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]);
}
}
}
}