Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Exclamation Array index Out of bound Exception

    Can anyone tell me how to know which array is throwing Array Index out of bound Exception. Actually i M Manipulating 15 2d Arrays Simultaneously with different upper and lower bounds. I m Confused which Array's index is going out of bound ?
    Please help. .. Reply fast

  2. #2
    arun9683 is offline Member
    Join Date
    Jun 2010
    Location
    Bangalore,India
    Posts
    70
    Rep Power
    0

    Default

    use arr.length to make sure you are not exceeding the arraylength

    Ex: for(int i=0;i<arr.length;i++)
    {
    for(int j=0;j<arr[i].length;j++)
    {
    String val = arr[i][j];
    some action

    }


    }
    Arun K R,Bangalore,India
    :)

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    which array is throwing Array Index out of bound Exception
    Often the source line number is in the error message.

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by arun9683 View Post
    use arr.length to make sure you are not exceeding the arraylength
    the length field returns the number of elements the array has been constructed. so if the length is 4 you can store 4 elements in the array but since the index is zero-based the index is from 0 to 3, the maximum index is always equals length - 1.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    Often the source line number is in the error message.
    This is the easiest way, if you want to fix the error. Error message contains all the required information if you read it carefully.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by nitin_daviet88 View Post
    I m Confused which Array's index is going out of bound ?
    How did you find that something is going wrong in your code?

  7. #7
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Another way to do it is to print messages at each crucial point (once per loop iteration for example) for each array as it is populated.

    It it not as efficient as checking the exception to see which line is causing the problem but it can often provide more information for you if for example you get your message to print out each value and it's position in the array as it is assigned.

    It can get messy though and you also need to make sure each message is uniquely identifiable so you know from which stage of the code the message originates.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Even printing the value on each iteration, if you found a null value end up with an exception.

  9. #9
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Even printing the value on each iteration, if you found a null value end up with an exception.
    Yes but it will let him know at which point the code is throwing the exception. (if he is struggling to read the exception message and find the error in the code)

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. [SOLVED] Array index out of bounds exception
    By sruthi_2009 in forum New To Java
    Replies: 6
    Last Post: 03-23-2014, 10:16 AM
  2. Array Index Out Of Bounds Exception
    By manowar689 in forum New To Java
    Replies: 3
    Last Post: 06-18-2010, 11:25 PM
  3. array Index out of Bounds exception== 0
    By Allgorythm in forum New To Java
    Replies: 6
    Last Post: 02-11-2010, 04:02 PM
  4. Array Index Out of Bounds Exception
    By kool001 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 07:42 AM
  5. Array Index Out of bound exception
    By abhijit in forum Networking
    Replies: 7
    Last Post: 09-25-2009, 07:25 PM

Tags for this Thread

Posting Permissions

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