0

I was told to use hashmap in this code to allow a XLSX file to be uploaded and be able to be read on the website. The data I key into the XLSX has to be captured on the website too. As for now, there's a null exception error.
This is how my code looks like now.

     OPCPackage pkg = null;
     XSSFWorkbook workbook = null;
     XSSFSheet sheet = null;

    try {
        pkg = OPCPackage.open("C:\\Users\\....Load_AcctCntr_Template.xlsx");
        System.out.println(_LOC + "1.0 " + " pkg:" + pkg);
        workbook = new XSSFWorkbook(pkg);
        System.out.println(_LOC + "1.0 " + " workbook:" + workbook);
        sheet = workbook.getSheetAt(Sheetindex);
        System.out.println(_LOC + "1.0 " + " sheet:" + sheet);
        pkg.close();
    } catch (Exception e) {
        System.out.println(_LOC + "1.0 " + " Test:");
        return _m;
    }   

    System.out.println(_LOC + "1.0 " + " sheet.getRow(0):" + sheet.getRow(0));
    int _totalc = sheet.getRow(0).getLastCellNum();
    int _totalr = sheet.getLastRowNum();


           // Header r=0
           String[] st = new String[_totalc];
           for (int c = 0; c < _totalc; c++) {
              st[c] = sheet.getRow(0).getCell(0).getStringCellValue();

    }

        _m.put("HEADER", st);
        System.out.println(_LOC + "1.0 " + " _m:" + _m);

        // Data r=1 thereafter
        List list = new ArrayList();
        for (int r = 1; r < _totalr; r++) {
            Object[] o = new Object[_totalc];
            String strRow = null;

            for (int c = 0; c < _totalc; c++) {
                o[c] = sheet.getRow(r).getCell(c);
                String cn = o[c].getClass().getName();
                String strCell = null;

                if (!isEmptyNull(strCell)) {
                    strRow = "record_available";
                }
            }

            if ((o != null) && (o.length != 0)) {
                list.add(o);

            }
        }
        _m.put("DATA", list);
        System.out.println(_LOC + "1.0 " + " _m:" + _m);
        return _m;
    }
    return _m;
}

What is shown on my console:

   SystemOut     O [RedirectLogin: requiredRights]1.0/BelsizeWeb/faces/module/entity/en1102.xhtml
   SystemOut     O [RedirectLogin: requiredRights]1.0en1102.xhtml
   SystemOut     O [En1102: doEn1102_command_readfileAction]1.0
   SystemOut     O [PageCodeBase: getConstructJXLList]1.0  pkg:org.apache.poi.openxml4j.opc.ZipPackage@a6742ad
   SystemOut     O [PageCodeBase: getConstructJXLList]1.0  workbook:Name: /xl/workbook.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml
   SystemOut     O [PageCodeBase: getConstructJXLList]1.0  sheet:Name: /xl/worksheets/sheet1.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml
   SystemOut     O [PageCodeBase: getConstructJXLList]1.0  sheet.getRow(0):<xml-fragment r="1" spans="1:7" x14ac:dyDescent="0.25" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:main="http://schemas.openxmlformats.org/spreadsheetml/2006/main">

 Caused by: java.lang.NullPointerException
at pagecode.PageCodeBase.getConstructJXLList_xlsx(PageCodeBase.java:6527)
at pagecode.module.entity.En1102.doEn1102_command_readfileAction(En1102.java:166)
52
  • 2
    To start with, the methods getCell, getRows and getColumns are not implemented and don't return any meaningful values. Once implemented please do more investigation and describe what exactly doesn't work for you. Try to restrict your question to the specific problem rather than posting a large block of code which doesn't work for some reason. Also when using Java try to follow Java naming conventions: java.about.com/od/javasyntax/a/nameconventions.htm and use meaningful names for your variables (_s_c is almost as expressive as _w_t_f) Commented May 29, 2014 at 8:58
  • @NorbertRadyk because these codes are pass down by the previous batch and I am suppose to edit it. So sorry for that. Initially, there isn't any methods for getCell, getRows and getColumns. But I added it because I was told not to use jxl and when I change some codes to be non-jxl, red underline appeared on getColumns, getRows and getCell saying that "The method getRows(),getColumns, getCell is undefined for the type PageCodeBase." So I tried to get the "help"/"quick fix" given and that's how I ended up with those methods. Commented May 29, 2014 at 9:06
  • Cassie, I'm trying to understand your question. What is PageCodeBase and where are you getting the errors you describe? There is so much going on here that you haven't shown us - I don't think you'll ever get a reasonable answer to the question in this form. Also, nobody prefixes variable names with underscores any more; it just makes everything much harder to read. Commented May 29, 2014 at 21:03
  • @DavidWallace PageCodeBase is the name of the java file. I'll try to type it clearly again (Really sorry as I am not good in coding and not good in explaining too). These are the codes for xls previously. So I changed the codes to make it work for xlsx. Previously, the line of code for int _totalc = getColumns(); suppose to be like this int _totalc = sheet.getColumns(); but I took away the sheet as the error said "method getColumns() is undefined for XSSFSheet." So I am wondering how do I solve this error of allowing getColumn to be defined for XSSFSheet. Commented May 30, 2014 at 1:28
  • 1
    OK, so you're getting an exception thrown, but because you're not printing it, we have no idea what it is. Can you please add e.printStackTrace(); immediately after } catch (Exception e) { then run it again and paste the output into the question? Commented May 30, 2014 at 10:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.