Import Excel .xlsx to Processing

Hello!
I have a problem about importing an excel file into processing. I followed this guide: Private Site
but in importExcel(), at this line:

Sheet sheet = wb.getSheetAt(0);

show me an error:

NullPointException.

How I solve this problem?
Help me! Please
Thank you all

1 Like

This probably means that wb has not loaded a spreadsheet, so the sheets list is null (there is no sheet 0). Perhaps your file is in the wrong place, or invalid, or is a version of excel that doesnā€™t work with this excel import code written before 2014.

On the page you linked there are comments. Someone had the same problem as you, and someone else suggested a way of formatting the file path. The file path format depends on whether you are on Mac or Windows, however.

The ā€œsolutionā€ comment has been autoformatted with smart quotes (curly) ā€“ breaking it. you will need to change them into straight quotes before trying it.

Beyond that, hard to know without seeing your code.

Thank you so much for the answer!
I followed the suggestion in the comment, but couldnā€™t solve the problem.

I have tried to write only the name of the Excel file, but it show me the error.
If I write the file path as described in the comment, it marks an error: Not expecting symbol ā€˜Uā€™, which is LATIN CAPITAL LETTER U.

This is my code in importExcel():

SXSSFWorkbook swb=null;
Sheet sh=null;
InputStream inp=null;
Workbook wb=null;

String[][] importExcel(String filepath) {
  String[][] temp;
  try {
    inp = new FileInputStream(filepath);
  }
  catch(Exception e) {
  }
  try {
    wb = WorkbookFactory.create(inp);
  }
  catch(Exception e) {
  }
  Sheet sheet = wb.getSheetAt(0);
  int sizeX = sheet.getLastRowNum()+1;
  int sizeY = 100;
  for (int i=0;i<sizeX;++i) {
    Row row = sheet.getRow(i);
    for (int j=0;j<sizeY;++j) {
      try {
        Cell cell = row.getCell(j);
      }
      catch(Exception e) {
        if (j>sizeY) {
          sizeY = j;
        }
      }
    }
  }
  temp = new String[sizeX][sizeY];
  for (int i=0;i<sizeX;++i) {
    for (int j=0;j<sizeY;++j) {
      Row row = sheet.getRow(i);
      try {
        Cell cell = row.getCell(j);
        if (cell.getCellType()==0 || cell.getCellType()==2 || cell.getCellType()==3)cell.setCellType(1);
        temp[i][j] = cell.getStringCellValue();
      }
      catch(Exception e) {
      }
    }
  }
  println("Excel file imported: " + filepath + " successfully!");
  return temp;
}

I donā€™t know how to solve this problem. What do you suggest me?
Thank a lot.

1 Like

Hi Angel96, on what system are you working?

because in Windows you must use backslash escapes for writing literal path:

String path = "C:\\Users\\Dennis\\Videos";
1 Like

Dennis thank you a lot, I was able to solve the problem of reading the excel file, with your suggested.
I didnā€™t think of writing the path for how you suggested to me.
How should write the path on mac?
thank you very much to both

Mhhā€¦ Now I canā€™t try but Tomorrow night I will do it on my iMacā€¦

Hi, sorry for lag responseā€¦
This is path in OS X

String path = "/Users/Dennis/Downloads/Processing.app"

keep us update

2 Likes