×

Discussion Board

Results 1 to 10 of 10

Thread: Vector Problems

  1. #1
    Nokia Developer Champion
    Join Date
    Apr 2007
    Posts
    2,708

    Vector Problems

    Hi,
    I've been using a Vector in my Midlet which holds information that gets updated every 10 seconds from a server. This Vector holds data which has to be shared with several other classes...
    I thought the Vector class is syncrhronized out of its own, so I also thought no kind of synchronization (several classes == several threads) was needed to acces and possibly change the data inside the Vector.
    Practically though it dosnt look this way, I am getting arrayoutofbounds exceptions, which are uncaught also... It tells me its with a Vector and because of the fact that im not using many vectors in this application im pretty sure its the one I just explained...
    The outofbounds exception could be because of a bad synchronziation of the data, worst case scenario :
    - class A requests the size of the Vecor to fill up a List object
    - cpu - switch : my Vector gets updated with new Servers data
    - cpu - swith : class A got his (outdated) Vector-size and starts iterating through vector to get the elements

    Well this will give a possible arrayoutofbounds exception...
    So what is a possible solution ? How to lock the Vector ? or should a local copy be made by class A so that it works on that copy with possibly outdated data ?

    Any hints, tips, help is appreciated, been stumbling with this problem for a while now...

  2. #2
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,405

    Re: Vector Problems

    Hi Tiger,

    you should use a 'synchronized' block to lock your Vector while you're updating its content, so that other parts of your app have to wait until it's fully updated to access it.

    Hope it helps
    Pit

  3. #3
    Nokia Developer Champion
    Join Date
    Apr 2007
    Posts
    2,708

    Re: Vector Problems

    hey jap,
    I allready did, the problem is that in a situation like I just depicted (class A requests size, and afterwards gets an element at a time) are atomary operations...
    So I also made a retrieveVector method, a addElementToVector method, and so on, all having the synchronized(Vector) variable as gatekeeper...
    Unfortunately its not as easy as that to tackle the problem

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    24

    Re: Vector Problems

    Hi
    ArrayOutofboundException is throw when you are accesing the unknown element id of the vector. ie., you are trying to access the element id not available in the vector, at that case only it will throw outofbound exception . its not in the case of synchronization.

    If you are not clear, please explain it briefly and to make it as clear.

    Check the size of the vector and the try to access the element id for 0 to size -1 . in that case u wont get any error



    Thanks and Regards
    Logesh.k

  5. #5
    Nokia Developer Champion
    Join Date
    Apr 2007
    Posts
    2,708

    Re: Vector Problems

    eehhrrmm..
    i definitely know what the error means, that wasnt the question :P

    because im trying to work with the copy of the vector, whilst the original one might have been changed in the meantime, it may be a sychronization problem...

    I thought my explanation was clear, so I actually dont get "If you are not clear, please explain it briefly and to make it as clear." comment...

    I did explain to make it clear, I am actually clear about the exception...

  6. #6
    Regular Contributor
    Join Date
    Mar 2003
    Location
    Barcelona, Spain
    Posts
    55

    Re: Vector Problems

    Have you tried to read the elements using an Enumeration?

    Code:
    Enumeration elements = vector.elements();
    
    while(elements.hasMoreElements() {
        Object obj = element.nextElement();
        // use obj
    }

  7. #7
    Nokia Developer Champion
    Join Date
    Apr 2007
    Posts
    2,708

    Re: Vector Problems

    could u explain what advantage that would have ?

  8. #8
    Regular Contributor
    Join Date
    Mar 2003
    Location
    Barcelona, Spain
    Posts
    55

    Re: Vector Problems

    You won't get ArrayIndexOutOfBoundsExceptions when accesing the elements of the Vector.

  9. #9
    Nokia Developer Champion
    Join Date
    Apr 2007
    Posts
    2,708

    Re: Vector Problems

    Is there a way to check the enumerations size, so the number of elements ? Or if it even has one element ?

    And if i am getting this correctly the Enumeration is only usable for read-only operations ? Lets say I'd like to add or remove an object within an Enumeration, how would I achieve this ?
    Last edited by Tiger79; 2008-04-22 at 13:22.

  10. #10
    Regular Contributor
    Join Date
    Mar 2003
    Location
    Barcelona, Spain
    Posts
    55

    Re: Vector Problems

    The only way to know the number of elements in an Enumeration is iterating with something similar to the loop I posted previously.

    And yes, the Enumeration is read only.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •