If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

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
Trusted Contributor
KJake
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

ArrayIndexOutOfBoundsException

I'm trying to remove an  element from a vector in persistent store, howver, keep getting an ArrayIndexOutOfBoundsException.  Perhaps something in the loop or am I not getting the index properly or not committing after removeElement or some other problem?

 

synchronized (store)
{
    //getting contents of the persistent object
    _data = (Vector) store.getContents();
    //checking for empty object
    if (!_data.isEmpty()) {
        for (int i = 0; i < _data.size(); i++) {
            StoreInfo info = (StoreInfo)_data.elementAt(i);
            String activateText = (info.getElement(StoreInfo.ActivateText));

            int a = _data.indexOf(info.getElement(StoreInfo.ActivateText));
                       
            Dialog.inform("Activate text " + activatetext);
            //now remove the element
            info.removeElement(a);
        }
    }
}

 

static {
    store = PersistentStore.getPersistentObject(0x870d2a35e0fbf6ceL);
    synchronized (store) {
        if (store.getContents() == null) {
            store.setContents(new Vector());
            store.commit();
        }
    }
    _data = new Vector();
    _data = (Vector) store.getContents();
}

private static final class StoreInfo implements Persistable
{
    private Vector _elements;
    public static final int ActivateText = 0;
    public static final int ActivateDate = 1;

    public StoreInfo()
    {
        _elements = new Vector(2);
        for (int i = 0; i < _elements.capacity(); ++i)
        {
            _elements.addElement(new String(""));
        }
    }

    public String getElement(int id)
    {
        return (String) _elements.elementAt(id);
    }

    public void removeElement(int idx)
    {
        _elements.removeElementAt(idx);
        //store.commit();
    }
}

 

 

Would really appreciate any feedback.  Thank you much.

 

Please use plain text.
Developer
simon_hain
Posts: 15,908
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: ArrayIndexOutOfBoundsException

do not remove elements from a collection you are currently iterating through.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Trusted Contributor
KJake
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: ArrayIndexOutOfBoundsException

hank you.  How then would I remove the element?  What would be the best approach?

Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: ArrayIndexOutOfBoundsException

Create a separate vector of element indexes to be removed. Once done with the rest of operations, delete elements going from the biggest index to the smallest.  This way the deletion does not alter the rest of the indexes.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.
Trusted Contributor
KJake
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: ArrayIndexOutOfBoundsException

Thanks for the reply.  Would you happen to have an example of this or maybe could point me to a reference?  I'm new to java and struggling.  Appreciate the feedback.  Thank you.

Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: ArrayIndexOutOfBoundsException

In your main loop, don't use removeElement immediately:

    if (!_data.isEmpty()) {
        for (int i = 0; i < _data.size(); i++) {
            StoreInfo info = (StoreInfo)_data.elementAt(i);
            String activateText = (info.getElement(StoreInfo.ActivateText));

            int a = _data.indexOf(activateText); // replaced for efficiency
                       
            Dialog.inform("Activate text " + activatetext);
            //now remove the element
            info.removeElement(a); // this is your problem - save your a in a Vector instead
        }
    }

 

Wrap that a into an Integer and add to a Vector of your own, checking if it is there already first. After the for loop is done, copy that Vector over into an array, sort that array with Arrays.sort(), then remove elements according to the indexes in that array going from the last to the first.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.
Trusted Contributor
KJake
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: ArrayIndexOutOfBoundsException

Thank you.  I appreciate the feedback.  Just out of curiousity, why copy the vector into an array to iterate through?  Can I just loop through the new vector and delete from the current?  I'm new to this and am tryingto figure out.  Also, could I still call the removeElement in the loop but wrap it in a thread to run later?  Would that work?  Having a difficult time adding to a new vector then copying to an array then iterating / sorting to delete in the persistent store vector. 

Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: ArrayIndexOutOfBoundsException

[ Edited ]

arkadyz is suggesting you store indicies in your new vector. Since you are using indexOf to get the indicies, you are not guarenteed they will be sorted. He is suggesting that you convert the Vector (or use IntVector instead) to an array so you can sort with the Arrays class. If you store the indices you need to iterate backwards or else your stored indices become invalid.

 

But, that aside, KJake is not removing from the Vector is iterating though. He is iterating through _data and removing from info.

 

Your real problem is actually here:

int a = _data.indexOf(info.getElement(StoreInfo.ActivateText));

 

You are pulling from info to get a String, and then getting the index of that String from _data... which will return -1 since it stores StoreInfo objects. Then you are removing index -1, which will throw the ArrayIndexOutOfBoundsException.

Please use plain text.
Trusted Contributor
KJake
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: ArrayIndexOutOfBoundsException

Thanks Matrin.  Are you suggesting that I can simply change the line

 

int a = _data.indexOf(info.getElement(StoreInfo.ActivateText));

 

to something else and  set 1 -= 1 to all me to delete the element without getting the error?  Is this doable?

 

Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: ArrayIndexOutOfBoundsException

I don't know what you just said, but if you keep your current code (what you originally posted) and just change "_data.indexOf" to "info.indexOf", it should work.

 

Although even that is extra work, so if you wanted to improve it just use "info.removeElement(StoreInfo.ActivateText);"... one step at a time though.

Please use plain text.