0

First, sorry for basic questions..But still, I could not run the run properly. The whole code is like this, and I run it with eclipse.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.hssf.usermodel.*;
public class hannimpeha1 {
    public static void main(String[] args) throws IOException {
         // Locate xls file.
                File workbookfile = new File("C:\\Users\\bok\\Desktop\\old.xls");
                FileInputStream file = new FileInputStream(workbookfile);
                POIFSFileSystem fs = new POIFSFileSystem(file);
                @SuppressWarnings("resource")
                HSSFSheet sheet = new HSSFWorkbook(fs).getSheetAt(1);
         // Get row and column count.
                    int rowCount = sheet.getPhysicalNumberOfRows();
                    int colCount = sheet.getRow(0).getLastCellNum();
         // Iterate over rows and columns.
                    for(int r = 0; r < rowCount; r++) {
                        HSSFRow row = sheet.getRow(r);
                      for(int c = 0; c < colCount; c++) {                         
                        HSSFCell cell = row.getCell(c);
                        String cellval = cell.toString();
                       System.out.print(" | " + cellval);
                      }
                      System.out.println(" |");
                  }
        }
}

The code itself seems okay, but it throws out,

 "Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap
    at hannimpeha1.main(hannimpeha1.java:15)" 

if I put XSSFRow row = null; to suppress error message, otherwise I got The local variable row may not have been initialized

what is this mean? and how can I fix this? Thank you for your suggestions.

Hannah Lee
  • 383
  • 2
  • 7
  • 19

2 Answers2

0

Add commons-collections4-x.x.jar file in your build path and try it again. It will work.

learner
  • 1,952
  • 7
  • 33
  • 62
  • I added it. but it wouldn't work – Hannah Lee Apr 13 '18 at 06:32
  • @HannahLee Has the error message at least changed? It should find the class if you add the correct jar to the class path. – kutschkem Apr 13 '18 at 06:41
  • Yes, it has changed. I also decide not using "xssf", instead, "hssf", still it gives, "Exception in thread "main" java.lang.NullPointerException at hannimpeha1.main(hannimpeha1.java:26)" – Hannah Lee Apr 13 '18 at 06:50
0

Local variables must be initialized with values in method otherwise declare the variable outside the method (instance variable).

Here is the link to download the jar file.

https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml/3.9