0

I am trying to create a program which reads the red value's from an image and stores the data into excel using Apache POI so i can create a histogram with it. However i'm getting a nullPointerException on the array i'm storing the data in when i try to pass the information to excel. The values in the array are taken from a 2d array which stores all the rgb values of the image. I have checked the array and the values are getting stored within it.

Do any of you guys know what would be causing this error?

The array is declared as:

static int[] redArray = new int[255];

And here is the code i am using to store the data into excel. The error occurs at cell.setCellValue(redArray[y]);

public static void chart() throws IOException {


    FileInputStream file = new FileInputStream(new File("chart.xlsx"));

    // Finds the workbook instance for XLSX file
    XSSFWorkbook myWorkBook = new XSSFWorkbook(file);

    // Return first sheet from the XLSX workbook
    XSSFSheet mySheet = myWorkBook.getSheetAt(0);

    // HSSFWorkbook workbook = new HSSFWorkbook(file);
    // HSSFSheet sheet = workbook.getSheetAt(0);
    // Sheet sheet=(Sheet) workbook.getSheetAt(0);
    Cell cell = null;

    // Update the value of cell

    for (int i = 1, y = 0; i < 255 && y < 255; i++, y++) {
        cell = mySheet.getRow(1).getCell(i);
        cell.setCellValue(redArray[y]); 
        System.out.println("asdasd");
    }


    file.close();

    FileOutputStream outFile = new FileOutputStream(

    new File("update2.xlsx"));
    myWorkBook.write(outFile);



    outFile.close();
    myWorkBook.close();
}

Thanks for the help.

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
  • Google `NullPointerException` in Java. There are plenty of resources to help you learn about how to fix this simple problem. – Kon Feb 21 '15 at 16:47
  • And in particular, read the exception stack trace, which tells you exactly where the exception is thrown. – JB Nizet Feb 21 '15 at 16:48
  • From the code you have provided, I can not see where you have given `redArray[y]` a value, which may be why a `NullPointerException` is being thrown; you haven't given the array any values. – James Feb 21 '15 at 16:49
  • The exception is not on the array, it's on the cell that the program is not finding to exist; you do not need to give a default value to the values in the array since they are primitive int data type. – Gregory Basior Feb 21 '15 at 16:50
  • My first guess would be, that i goes out of range and then `mySheet.getRow(1).getCell(i)` returns null, which in returns causes `cell.setCellValue(redArray[y])` to fail. – reckter Feb 21 '15 at 16:50
  • You can probably post the stack trace to identify the exact issue. – Kartic Feb 21 '15 at 16:51

0 Answers0