×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Unhappy * For loop result in an very wierd exception...

    I have this very strange problem, i have this piece of code you're seing below. keysSet is a vector which has a size always greater than 0 for sure, currently it's always 24.

    for (int i = 0; i < keysSet.size(); i++) {
    if (i < keysSet.size()) {

    IF i remove the second line of code, this loop will work sucessfully but finally it gets a null pointer exception because in the third line i'm accessing:

    keysSet.elementAt(i);

    and obviously there is no such element at that id...

    Any help will be highly appreciated, because it doesn't make a little piece of sense, because it's the normal logic of the loop to stop if i is smaller than keysSet.size() and it shouldn't go down the first line after it realise that i is not smaller than keysSet.size()

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: * For loop result in an very wierd exception...

    A NullPointerException is not indicating that you are accessing past the end of the Vector (you would see an IndexOutOfBoundsException). However, that doesn't explain why the "if" fixes the problem. Something else is happening here. Can you reproduce the problem in a small code fragment, and post it here? "for" loops and Vectors are not broken, so something is happening elsewhere in your code. Is keysSet being accessed from another thread? What is in the rest of the loop?

    Graham.

  3. #3
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Re: * For loop result in an very wierd exception...

    Sorry for the mistake because currently i'm a bit sick and my mind is a bit slow heh

    I'm getting an index out of bounds exception not a null pointer exception, thats why the if statement fix the problem and i confirmed that when i debugged the code line by line.

    Can you explain why this index out of bounds occur because it should never occur in a for loop...

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: * For loop result in an very wierd exception...

    Quote Originally Posted by kurteknikk View Post
    Can you explain why this index out of bounds occur because it should never occur in a for loop...
    Quote Originally Posted by grahamhughes View Post
    Can you reproduce the problem in a small code fragment, and post it here? "for" loops and Vectors are not broken, so something is happening elsewhere in your code. Is keysSet being accessed from another thread? What is in the rest of the loop?
    I refer you to my previous reply... need to know more information.

    Graham.

  5. #5
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Re: * For loop result in an very wierd exception...

    Ok i'm going to show you the whole function...

    openRecStore(dbAppData);
    for (int i = 0; i < keysSet.size(); i++) { // then insert the updated fields in dbAppData
    if (i < keysSet.size()) { //TODO index out of bounds exception, why is it occuring ?
    if (keysSet.elementAt(i) != null) {
    writeRecord(new String((String) keysSet.elementAt(i)).getBytes((String) keys.elementAt(0)));
    } else {
    writeRecord(new String("").getBytes((String) keys.elementAt(0)));
    }
    }
    }
    closeRecStore();

    Basically i'm opening a record store, reading writing all the data in the vector keysSet to the record store and closing it normally.

    The problem i was having without the if statement was that it was continueing and checking if keysSet.elementAt(i) != null but obviously that particular element doesn't exist and i was having an index out of bounds exception :s

    I hope i am clear enough if you need more information just ask and thanks a lot in advanced for your help

  6. #6
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: * For loop result in an very wierd exception...

    This problem happens in the emulator?

    Is the object referenced by keysSet accessed from another thread?

    When you debug this, what is the value of "i" when the exception is thrown, and what is the size of the Vector at that point?

    Graham.

  7. #7
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Re: * For loop result in an very wierd exception...

    This problem happens in both the emulator and the phone.

    The size of i will be 24 and the size of the vector is 24 because obviously i gets incremented but instead of going out of the loop it goes to the next line for some reason :s

    And by the way the object referenced by keysSet is not accessed from another thread...

  8. #8
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: * For loop result in an very wierd exception...

    Quote Originally Posted by kurteknikk View Post
    The size of i will be 24 and the size of the vector is 24 because obviously i gets incremented but instead of going out of the loop it goes to the next line for some reason :s
    "Will be" sounds like you haven't checked. Don't make assumptions.

    Actually, the exception message usually has the form "x >= y" (where "x" is the index value given, and "y" is the size of the Vector)... do you see such a message?

    If you can't find the information in the debugger, try adding:

    Code:
    System.out.println("i: " + i + ", size: " + keysSet.size());
    at the first line in the loop.

    Graham.

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    4,105
    A vector is a vector (and might not be a static array). For iterating over a vector you have to use Enumeration. Period.

    Your vector index might not be used at that moment. The array might be gone. … Learn more about Vectors in the phoneME project. Sun’s trick is to synchronize the Vector object. Anyway, Enumeration is yours.

  10. #10
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Re: * For loop result in an very wierd exception...

    Hi friends,

    First of all sorry but i was really sick and busy...

    So... i didn't make any assumptions it was just the way i said it heh i'm sure of the value of i etc...

    Also i'm working with the vector and i found the enumaration you told me, to tell you the truth i never used it before.

    I would like to ask you a specific question about enumeration in vectors.

    So i'm using keysSet.elements() to get the enumartion, will that bring an enumeration of all components which are not null ?

    Because if it will bring even the elements which are null i think that i'll be having the same result like before... :-/

  11. #11
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: * For loop result in an very wierd exception...

    The enumeration will return all values from the Vector, null or otherwise. However, you said you are experiencing an IndexOutOfBoundsException, not a NullPointerException, so returning null will not result in the same problem.

    Graham.

  12. #12
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Re: * For loop result in an very wierd exception...

    Yes you're right mate, but this is a bit of a more complicated application heh

    I've got various for loops, i've got 2 for reading records from a record store and another 2 for writing to the record store.

    The problem is that when i'm going through the vector to save the keys i'm having this index out of bounds if i don't add the if statement on the second line.

    But before trying to save the keys i wanted to know if there are keys to save, because if all keys are null they shouldn't be saved, but if there's at least 1 which is not null, then they should all be saved. And i fixed this by creating an enumeration and if any 1 of the components is not null it breaks the loop and saves all the keys.

    Actually i've just solved the problem with enumeration for saving the keys, but still when reading them, since i'm using a for loop i should have that if statement to make sure it doesn't break !

    I can't figure out why such a simple loop can create all this hassle

  13. #13
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: * For loop result in an very wierd exception...

    Then... why not follow traud's advice and use only an Enumeration to iterate over the Vector, and not the for() loop?

  14. #14
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Re: * For loop result in an very wierd exception...

    Because i need to specifically save keys.elementAt(1) to record id 1 etc... Because i'm dealing with keys which dyamically setup all my application so if i skip just 1 id i will make a big disaster.

    So it's better to have an exception and i will know that there was a problem reading keys than not knowing and at least 1 key gets misplaced because then i'll have everything misplaced and my application won't even load...

    I don't know if you're getting what i'm trying to say, but these keys have a very high priority in my application because they set encoding, all values on screens, and other values needed for the application to operate such as timer delay etc...

  15. #15
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: * For loop result in an very wierd exception...

    The Enumeration won't skip elements. It will return each element of the Vector in turn.

    (Personally, I wouldn't save each element in a separate record, I'd pack them all into one record, but that's a different matter.)

    I can't help you more with the for() loop problem without seeing more code (probably the entire application). The expression (i < keysSet.size()) cannot return true on one occasion and false on another, without the value either of i or of keysSet.size() having changed, and a for() loop will not enter the statement body unless the control expression evaluates to true.

    Graham.

Similar Threads

  1. s60 3rd ed emulator crash
    By Kimau in forum Symbian Tools & SDKs
    Replies: 2
    Last Post: 2008-06-18, 22:12
  2. VS.Net 2003 Carbide 2.01 and epoc32.exe
    By ValentinK in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 2
    Last Post: 2007-01-12, 12:31
  3. Catching an exception
    By btnokdev in forum Mobile Java General
    Replies: 2
    Last Post: 2003-10-11, 14:45
  4. can not successfully link any sample using .NET
    By lobotomat in forum Symbian Tools & SDKs
    Replies: 2
    Last Post: 2002-08-20, 00:29

Posting Permissions

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