Results 1 to 7 of 7
  1. #1
    ashvini is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Why I am getting null pointer exception?

    Hi Team,

    I wrote a program to read 'row count' and 'last cell num'. Please see below code.
    I am not getting NullPointer error at row no. 19 (bold and red colour) error while compile but during runtime, I am getting following error:

    // Error Message reads:
    Num. of rows3
    Exception in thread "main" java.lang.NullPointerException
    at com.xl.read.PoiXLRead.main(PoiXLRead.java:19)


    /// Code:
    package com.xl.read;

    import java.io.FileInputStream;
    import java.io.IOException;

    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFRow;

    public class PoiXLRead {

    public static void main(String[] args) {
    try {
    FileInputStream FSRead = new FileInputStream("/Users/ashv/ReadXLfile/Input/Input.xlsx");
    XSSFWorkbook WB = new XSSFWorkbook(FSRead);
    XSSFSheet Sh = WB.getSheet("TestData");
    int rows = Sh.getPhysicalNumberOfRows();
    System.out.println("Num. of rows" + rows);
    int cellnum = Sh.getRow(rows).getLastCellNum();
    System.out.println("Num. of last col" + cellnum);
    }catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: Why I am getting null pointer exception?

    Please use [code] tags [/code] when posting code so that it retains its formatting.

    PhysicalNumberOfRows returns a count of rows containing data, not a count of the last row number.
    In addition in Excel not all rows actually exist, so rows 1-10 might contain data and rows 12-20, but row 11 may not. Getting row 11 will result ni a null Row object being returned.

    Since I guess this is related to your other post in the POI forum you need to get the row iterator from the sheet and loop over the rows looking for the largest LastCellNum.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    ashvini is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: Why I am getting null pointer exception?

    Thanks for your response Tolls. I will make sure to put tag.
    However, I am not convince with reason of NullPointer exception. Here is the another way:
    Java Code:
    		int col = Sh.getRow(3).getPhysicalNumberOfCells();
    Still I am getting NullPointer exception.
    what I changes is, I placed constant 3 rather than variable rows and used method getPhysicalNumberOfCells(); insted of getLastCellNum();.
    And, I used same method "getLastCellNum();" for row (please see above code) and it returns correct value which is "Num. of rows3".
    So why this method is not returning me correct result here.
    I just want to get the number of Physical Number Of Cells in last row of Excel sheet.
    (Please do not co-relate it with my other query, this is new which I encountered during something else.)

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Why I am getting null pointer exception?

    Hi ashvini,

    Arrays start at index 0 instead of 1. For example an array with a length of 3 would have indexes 0, 1, 2.
    In your code you are asking for the number of physical rows but this value is one greater than the last actual index.

    Regards.

  5. #5
    ashvini is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: Why I am getting null pointer exception?

    Oh! got it.
    I did rows-1 and its work. Thank you.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: Why I am getting null pointer exception?

    As I said, though, there is a row iterator you should be using if you need to get the largest column number (which is what I thought you were going for)?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    ashvini is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: Why I am getting null pointer exception?

    I am sorry, I did not understand easily. I am not programming guy, I have started learning recently.
    So far, I am a functional and core person. And, What I understood is, in above case Java runtime is throwing wrong error message and it should work even with only 'rows', no need of -1. However, I got workaround of using 'rows-1' (that you suggested through array pointer) so I am good to move ahead.

Similar Threads

  1. Need Help With Null Pointer Exception
    By kendraheartt in forum New To Java
    Replies: 4
    Last Post: 08-16-2012, 05:29 PM
  2. null pointer exception help
    By captain_turkiye in forum New To Java
    Replies: 17
    Last Post: 12-04-2011, 12:27 AM
  3. null pointer exception
    By Herah in forum New To Java
    Replies: 1
    Last Post: 12-01-2011, 08:44 AM
  4. null pointer exception
    By marvelk in forum Advanced Java
    Replies: 8
    Last Post: 02-01-2011, 09:02 AM
  5. null pointer exception
    By jyothi.priyanka in forum New To Java
    Replies: 12
    Last Post: 03-11-2009, 05:04 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
  •