Results 1 to 4 of 4
Like Tree2Likes
  • 2 Post By gimbal2

Thread: Get POI cell value results

  1. #1
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default Get POI cell value results

    Hi,

    I'm currently using Apache POI to read value from an excel doc and modifying the value into lines of mySQL syntax.
    So far I've managed to get the value and print them using System.out, however when I tried to build a string (using StringBuilder) or placing them into arraylist, it doesn't seem working and always gives a null pointer exception error.

    Here's some of the codes :

    Java Code:
        private short getLastCol(XSSFSheet sh) {
            short lastcol = 0;
            for (int x = 1; x <= sh.getLastRowNum(); x++) {
                if (lastcol < sh.getRow(x).getLastCellNum()) {
                    lastcol = sh.getRow(x).getLastCellNum();
                }
            };
            return lastcol;
        }
    Java Code:
        private void readxlsx(File file) {
            try {
                System.out.println("File : " + file.toString());
                fis = new FileInputStream(file);
                XSSFWorkbook wb = new XSSFWorkbook(fis);
                XSSFSheet sh = wb.getSheetAt(0);
                int n = getLastCol(sh);
                System.out.println("row : " + sh.getLastRowNum());
                System.out.println("column : " + n);
                for (int x = 1; x <= sh.getLastRowNum(); x++) {
                    for (int y = 0; y < n; y++) {
                        if (sh.getRow(x).getCell(y) != null) {
                            System.out.println(sh.getRow(x).getCell(y).getStringCellValue());
                            sb.append(sh.getRow(x).getCell(y).getStringCellValue());
                        }
                    }
                }
                System.out.println("string builder : " + sb.toString());
            } catch (IOException ex) {
                Logger.getLogger(XLtoDB.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(XLtoDB.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    The above code works well if I don't put that stringbuilder sb into it, but gives me error when it is there
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException


    I wonder what the problem is, can anyone help me with this ?


    Thanks n regards,
    First is to make something usable, next is to aim for perfection which means a never ending improvements.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,733
    Rep Power
    5

    Default Re: Get POI cell value results

    Yes, so when do you get a NullPointerException? I'm not going to say anything until you take the time to research that and be able to explain it in your own words.

    And by then you will be able to solve this yourself.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default Re: Get POI cell value results

    Ah thanks,

    Figured it out...there's one basic thing that I forgot after not doing coding for some time
    Thanks for the "when" stuff, reading that is like hammering my head for forgetting something quite basic, really should ask myself before asking next time.


    Thanks a lot,
    First is to make something usable, next is to aim for perfection which means a never ending improvements.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,733
    Rep Power
    5

    Default Re: Get POI cell value results

    Remember this feeling you must have now: you did it yourself, victory. That feeling of success is what kept me going when learning this stuff myself; if I didn't have that I wouldn't have persisted in this field I can tell you.
    Levian and DarrylBurke like this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. How do I put my results in GUI?
    By dynamix in forum AWT / Swing
    Replies: 6
    Last Post: 03-17-2013, 11:33 PM
  2. How do I put my results in GUI?
    By dynamix in forum SWT / JFace
    Replies: 1
    Last Post: 03-17-2013, 03:39 PM
  3. read excel cell value using cell id or name
    By Ambika in forum Apache POI
    Replies: 0
    Last Post: 02-22-2013, 12:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •