Results 1 to 6 of 6
  1. #1
    cfc1252 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Array out of bounds

    Below is a similar example to my code. I have an array where some of the elements are empty. When i try and check to see if the element is null, I get the following error: "java.lang.ArrayIndexOutOfBoundsException: 2" All I want to do is if the element fields[2] is empty skip over it if not then set String t = fields[2];
    Java Code:
    fields[0] = "Something";
    fields[1] = "Something else"; 
    fields[2] = "";
    
    String s = fields[0]; 
    String e = fields[1];
    
    if(fields[2] != null){
       String t = fields[2]
    }
    
    // Do something with "s" "e" "t"

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default Re: Array out of bounds

    Show us the initialization of your 'fields' array; I bet it has only two elements.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    cfc1252 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: Array out of bounds

    The array is for a regex string split.

    Java Code:
    String fields[] = line.split(regex);

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Array out of bounds

    Confirm that your array has enough elements using the last index:
    array.length-1

    Output that value so you can see for yourself.

    Whats probably happening is that your regex string is not splitting the input how you expect it,
    so the second element in your array might be merged with the value of what shouldve been the
    third item in the array.

  5. #5
    cfc1252 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: Array out of bounds

    I think I have worked out the problem. The strings I am splittling have a different number of parts i have splittling them into.

    For example the first string has fields[0], fields[1] and fields[2]. Where as string two will only have fields[0] and fields[1]. The code in my orginal post is surrounded by a loop while there is a new line. So when i try and access field[2] for string 2, it can't because there is no fields[2] to access. So my question is how can i skip over fields[2] if there isnt one?

  6. #6
    cfc1252 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: Array out of bounds

    Solved!! Thanks for the replies anyway!

Similar Threads

  1. Out of Bounds Array...sometimes
    By lampposteffect in forum New To Java
    Replies: 12
    Last Post: 04-21-2011, 07:10 PM
  2. 2d Array, Array out of bounds exception
    By Bmorebob in forum Advanced Java
    Replies: 2
    Last Post: 03-24-2011, 09:34 PM
  3. array going out of bounds?
    By jabo in forum New To Java
    Replies: 9
    Last Post: 04-02-2010, 10:08 AM
  4. Array out of bounds exception 20.
    By dropt in forum New To Java
    Replies: 4
    Last Post: 09-21-2009, 10:32 PM
  5. why is my array out of bounds?
    By Phobos0001 in forum New To Java
    Replies: 3
    Last Post: 03-24-2008, 01:20 AM

Posting Permissions

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