Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Developer
Posts: 283
Registered: ‎07-22-2008
My Device: Not Specified

ArrayIndexOutOfBoundsException in 4.6

I upgraded to the latest JDE 4.6. But I am getting the exception: ArrayIndexOutOfBoundsException  when adding a list field derived from an ObjectListField to a vertical manager.

This code works fine in 4.2, but generates this exception on both the simulator and the device in version 4.6.

 

I remember that 4.6 beta version was not even compiling code that dealt with ObjectListField.

Any clue?

 

Thank you.

Developer
Developer
Posts: 283
Registered: ‎07-22-2008
My Device: Not Specified

Re: ArrayIndexOutOfBoundsException in 4.6

Any clues?

The problem is still happening on the device and on the simulator.

 

Thanks

Developer
Developer
Posts: 283
Registered: ‎07-22-2008
My Device: Not Specified

Re: ArrayIndexOutOfBoundsException in 4.6

For more info here is a partial list of the stack when the crash happens.

 

Vector.elementAt(int)

ObjectListField.measureListRow(ListField,int,int)

ListField.layout(int,int)

Manager.layout(Field,int,int)

 

Checking the doc, I found that the ObjectListField in 4.6 implements an additional interface : ListFieldMeasureCallback

that was not in version 4.2. 

 

Could this be the problem ?

 

Thanks

BlackBerry Development Advisor
Posts: 15,302
Registered: ‎07-09-2008
My Device: BlackBerry Passport
My Carrier: Bell

Re: ArrayIndexOutOfBoundsException in 4.6

Are you seeing this in version 4.7.0 of the BlackBerry JDE as well?  Can you provide a larger code sample that triggers this exception?
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Developer
Posts: 283
Registered: ‎07-22-2008
My Device: Not Specified

Re: ArrayIndexOutOfBoundsException in 4.6

After a lot of testing I confirmed that this is a bug in 4.6.

It can be reproduced with a very simple example independent of my application.

The problem happens when we extend the ObjectListField. It looks that it is happening in the new interface that they added to the class. Extending the ListField itself works fine.

 

I did not get chance to test it on 4.7. I will post the result as soon as I get to it within the next day or so.

 

 

Thanks

Developer
Developer
Posts: 283
Registered: ‎07-22-2008
My Device: Not Specified

Re: ArrayIndexOutOfBoundsException in 4.6

Yes, I tested the 4.7 version and it has the same problem as 4.6. THe exception is still there when you extend an objectListField.

 

Here is a sample code to reproduce the problem

 

 

public class ListFieldImp extends  ObjectListField
{
    private Vector _elements = new Vector();
        
    /**
     * Constructor.  Sets itself as the callback.
     */
   public  ListFieldImp() {
        setCallback(this);
     }

public Vector getElements()
  {
     return _elements;
  }
 
    /**
     * Adds the provided element to this list field.
     *
     * @param element The element to be added.
     */
    public void add(Object element) {
        _elements.addElement(element);

    setSize(getSize());
       }

 

/**
   * <description>
   * @param listField <description>
   * @param graphics <description>
   * @param index <description>
   * @param y <description>
   * @param width <description>
   */
   
   public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width)
   {
        if (index >=  getSize() )
            return;

String item = (String)_elements.elementAt(index);
         
        int ypos = 10 +y;
        int rowHeight = this.getFont().getHeight();
        int xpos = 10;
        graphics.drawText(item, xpos, ypos);
         ypos += rowHeight ;
       
        //draw seperator line across bottom of the row
        graphics.setColor( Color.DARKGRAY );
        graphics.drawLine( 0, y + rowHeight - 1, width, y + rowHeight - 1 );
    }


       public Object get(ListField listField, int index) {
        if (index >= 0 && index < getSize())
        {
            return _elements.elementAt(index);
        }
        return null;
    }


    // see JavaDocs for ListFieldCallback.getPreferredWidth()
    public int getPreferredWidth(ListField listField) {
        return Graphics.getScreenWidth();
    }


    // see JavaDocs for ListFieldCallback.indexOfList()
    public int indexOfList(ListField listField, String prefix, int start) {
        return listField.indexOfList(prefix,start);
    }


   protected   void layout(int width, int height)
    {
        super.layout(width,height);

}
    
            
    
    /**
     * Retrieves the number of elements in list field.
     *
     * @return The number of elements in this list field.
     */
    public int getSize() {
        return (_elements != null) ? _elements.size() : 0;
    }
   
   
    /**
     * Removes the element at the provided index from this list field.
     *
     * @param index The index of the element to remove.
     */
    public void remove(int index) {
        _elements.removeElementAt(index);
        setSize(getSize());
    }


    /**
     * Removes all elements from this list field.
     */
    public void removeAll() {
        _elements.removeAllElements();
        setSize(0);
    }
   
}

 

To test the list field:

 

 

final public class ListScreen extends MainScreen
{
   public ListFieldImp      _list     = new ListFieldImp();


   
    /**
     * Constructor.
     */
    public ListScreen() {
        setTitle("List Test");

           
        _list = new ListFieldImp();
        _list.setEmptyString("",0);        
        add(_list);
       
       _list.add(" This is entry 1");
       _list.add(" This is entry 2");
       _list.add(" This is entry 3");
       _list.add(" This is entry 4");
      }


    /**
     * Overrides default.  Removes listener before closing the screen.
     */
    public void close() {
            super.close();
    }
}

BlackBerry Development Advisor
Posts: 15,302
Registered: ‎07-09-2008
My Device: BlackBerry Passport
My Carrier: Bell

Re: ArrayIndexOutOfBoundsException in 4.6

The issue is caused by the fact that the object array internal to the ObjectListField is empty.  You can work around this by calling this.set(obj) in your add and remove methods, passing in an object array taken from your Vector.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Developer
Posts: 283
Registered: ‎07-22-2008
My Device: Not Specified

Re: ArrayIndexOutOfBoundsException in 4.6

Is this a problem that will be fixed in the future? or the new behavior of the objectListField?

 

Thanks