Results 1 to 3 of 3
  1. #1
    athar is offline Member
    Join Date
    Feb 2013
    Rep Power

    Default How to count list size?

    I have a list and i have added class object to list. My code is as follows
    public class HeaderReason
    private String sRejCode;
    private String sDescription;

    public void setCode(String sCode)
    sRejCode = sCode;
    public String getCode()
    return sRejCode;

    public void setDescription(String sDesc)
    sDescription = sDesc;
    public String getDescription()
    return sDescription;

    public class HeaderReasons extends ArrayList<HeaderReason>


    I have added items to list as below code
    objHReason = new HeaderReason();
    objHReason.setDescription("Rejected for more than one reason.");

    When return lstHReasons and check the size it is showing me as 2 but it contains only one record at [0]. When i loop through this list as getting error as nullpointer

    stmt = conSqlServer.prepareStatement(sqlQuery);
    for(int i = 0; i < lstHReasons.size(); i++)
    stmt.setString(1, sEstdFileRefNo);
    stmt.setString(2, lstHReasons.get(i).getCode());
    stmt.setString(3, lstHReasons.get(i).getDescription());
    How to loop through this list and add contents to database?

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Rep Power

    Default Re: How to count list size?

    Hi athar,

    The code is not complete enough to say where your issue lies but you said you have added items to the array as opposed to just a single object.
    Can you provide the complete code you have used to add the items to the list.


  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: How to count list size?

    Please wrap your code in [code] tags [/code] so that it retains its formatting.
    Many of us won't bother reading unformatted code.

    Next, please post the full exception including stack trace, highlighting the line on which it is thrown.

    Finally, presuming it's this line:
    Java Code:
    stmt.setString(2, lstHReasons.get(i).getCode());
    can you post the code (as Ronin asks) in which you populate the list?
    Looks to me like somewhere you are adding a null into the list.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. count every 45 records and prfix the count number
    By dkr786 in forum New To Java
    Replies: 8
    Last Post: 02-15-2013, 08:31 PM
  2. count elements in linked list recursively!
    By hussien in forum New To Java
    Replies: 2
    Last Post: 11-23-2012, 06:17 PM
  3. How would I count number of nodes in linked list?
    By colerelm in forum New To Java
    Replies: 2
    Last Post: 10-17-2011, 07:26 AM
  4. java 2 dimensional no size list
    By rblmvs in forum New To Java
    Replies: 6
    Last Post: 04-03-2011, 11:11 AM
  5. Setting frame size to the size of an image
    By Yoruichi in forum AWT / Swing
    Replies: 5
    Last Post: 04-22-2009, 05:37 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