Results 1 to 4 of 4
  1. #1
    nitishjtm is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Post ArrayIndexOutOfBoundException

    Hi,
    I am new to java.
    I was performing a java program which compiled successfully but when I run the program the following message appeared on the command prompt

    "java DifferentIfTest
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
    at DifferentIfTest.main(DifferentIfTest.java:4)"

    I am not understanding what does it mean.
    Please help me.........
    Thank You.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default

    Quote Originally Posted by nitishjtm View Post
    Hi,
    I am new to java.
    I was performing a java program which compiled successfully but when I run the program the following message appeared on the command prompt

    "java DifferentIfTest
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
    at DifferentIfTest.main(DifferentIfTest.java:4)"

    I am not understanding what does it mean.
    Please help me.........
    Thank You.
    Suppose you have an array of five elements; the valid index values are 0, 1, 2, 3 and 4 all other index values throw this Exception. The Exception message tells you on which line it happened and in which method.

    kind regards,

    Jos

  3. #3
    bassfero is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    it's simple

    you are using a array somewere in your code, and your are trying to access element out of array

    for example:
    Java Code:
    byte[] sample_array = new byte[3];
    System.out.println(sample_array[4]);
    will give you same exception

    post a code here, or check places where you are accesing element in array

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    10

    Default

    To make the reason for these kinds of exceptions even clearer that bassferos example, the following code produces a AIOOB exception:
    Java Code:
    int[] arr = new int[3];
    System.out.println(arr[3]);
    Remeber that array indexes start at 0, not 1! An array also has a public field called length, whitch can help avoid these issues. Every time you want to do something with the array, check the index against this field:
    Java Code:
    public class MyClass {
      private int[] arr;
      public MyClass(int numElements) {
        arr = new int[numElements];
      }
    
      public int getElementAt(int index) {
        [B]if(index < arr.length && index >= 0)[/B] return arr[index]; //check the validity of the index provided
        return -1; //error
      }
    }
    Ever seen a dog chase its tail? Now that's an infinite loop.

Posting Permissions

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