relation between Arraylist and ArrayIndexOutOfBoundsException

What is the relation between List and Array. Why i am getting ArrayIndexOutOfBoundsException.?

Basically i am passing one argument to a method, based on that using HQL, i am returning one set of ArrayList. Now my problem is that. when this ArrayList returns more than 0 (size) this is working exactly the way i want.

But when it returns 0 (size) ArrayList, i am getting Exception. why is that. can any body explain to me

Now i got one more doubt like if an arraylist returns 5(first) & 0(second as size i tested up to) i am not getting any Exception. but when it returns like 200 or more elements its getting exception. why is that? Is there any specific constant no. like up to this many elements array shouldn't give IndexOutofBounds and things like that? can any body explain to me?

here is my code:

  public void setUpDisplay()
    if (_flatView || _expired || _issue){ // these are all my views.
        _deptBalances = fetchBalances(null);
    else if (_searchGen.getGenericName() != null){ 
        _deptBalances = fetchBalances(_searchGen.getGenericName());
        if (!ListUtil.nullOrEmptyList(_deptBalances)){
            // i am displaying the result here.
        else {
            displaying error message.
      // here i am using my _deptBalances to display(i am just putting this list into displaygroup).

// here is my method.

    public List<DEPTStoreBalance> fetchBalances(String genName){
    EntityManager em // Creating instance to entity manager.
    List <DEPTStoreBalance> tempList = ListUtil.list();
    String expClause =  new String();
    if (_expired){
        expClause = "and gg.bSubjectToExpiration=true " +
                    "and msb.expirationDate <= :expDate ";
            expClause = expClause.concat(" and msb.expirationDate > :today");
        else {
            expClause = expClause + 
                        "and (msb.expirationDate > :today " +
                        "or (balance.qtyBalance > 0 or balance.qtyInTransit > 0)) ";
    else {
        expClause = "and ((gg.bSubjectToExpiration=true " +
                    "and msb.expirationDate > :expDate) " +
                    "or gg.bSubjectToExpiration=false) ";

        if (_flatView || _issue){
            expClause = expClause.concat("and (balance.qtyBalance > 0 or balance.qtyInTransit > 0) ");
        else if (genName != null){
            expClause = expClause.concat("and gg.genericName = :genName ");

    String hql = "select balance from DEPTStoreBalance as balance " +
                 " "+ // here are my joins with multiple tables.
                 "where = :store " +

    if (_issue)
        hql = hql.concat(" and dsi.deptIssue = :deptIssue");
    Query q = em.createQuery(hql);
    q.setParameter("store", _store); // here i am selecting the store(which is being changing in search component).
    if (_issue)//Only saleable items should be issued
        q.setParameter("expDate",12 months);
        q.setParameter("expDate",_minExpDate ); // constant value :3
    if (_expired)
        q.setParameter("today", new Date());
    if (genName != null){
        q.setParameter("genName", genName);
    if (_issue)
        q.setParameter("deptIssue", true);

        tempList = (List <DEPTStoreBalance>) q.getResultList();
    catch (NoResultException nre){
        //do something
    finally {
    return tempList;        


Arraylist is a dynamic array. Array has fixed size where as Arraylist is not.

Example: if you declare array as

int[] arr = new int[5];

you have to provide the size of an array.

  ArrayList al = new ArrayList();
  if(al.size()>0) { 
   // do your things 


An ArrayList is nothing but a dynamically growing array.

You're experiencing an ArrayIndexOutofBoundException because when you receive an empty list and you're trying to access a particular location which is non-existent, JVM throws you an exception complaining that you've crossed the bound of the internal array a.k.a ArrayIndexOutOfBoundException.

For example, in your case, when you've an empty ArrayList, the size of the list would be 0. However if you try to access an index which is >= 0, JVM will throw an ArrayIndexOutofBoundException

ArrayList internally uses array to store its content. ArrayList is basically sort of dynamic array.

If you try to access an element at index which is not available, you will get ArrayIndexOutofBoundsException.

If your List has, say, 3 members and you are calling list.get(5) the exception is thrown. It is because the ArrayList is implemented by Java array.

Need Your Help

Using Dojo 1.9, all parsing fails in IE

javascript internet-explorer parsing dojo

So I have a rather large web app that was running Dojo 1.8. Everything works fine in multiple versions of IE and Firefox. I decided to give 1.9 a go and changed my dependency from 1.8 to 1.9. Fi...

To maintain the state of a scrolling level menu

objective-c xcode menu cocos2d-iphone

Iam working on a game in which I have created a horizontal scrolling menu for levels using CCScrollLayer.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.