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

    Default How to count list size?

    Hi
    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.setCode(sCode.toString());
    objHReason.setDescription("Rejected for more than one reason.");
    lstHReasons.add(objHReason);

    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());
    stmt.addBatch();
    }
    stmt.executeBatch();
    How to loop through this list and add contents to database?
    Thanks

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

    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.

    Regards.

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

    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, 07:31 PM
  2. count elements in linked list recursively!
    By hussien in forum New To Java
    Replies: 2
    Last Post: 11-23-2012, 05: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, 06:26 AM
  4. java 2 dimensional no size list
    By rblmvs in forum New To Java
    Replies: 6
    Last Post: 04-03-2011, 10: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, 04: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
  •