Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: ArrayIndexOutOfBounds

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default ArrayIndexOutOfBounds

    Ok so I'm working on this program which passes the elements on an array into an array object. It compiles but I keep getting an ArrayIndexOutOfBoundsException: 0 error.
    Its happening at tempArray[index] = i; but I can't seem to figure out how it's out of bounds. Any thoughts?

    public class MyArray {
        private int size;
        private int[] tempArray = new int[size];
        private int index = 0;
     
        public MyArray(){
            size = 15;
        }
     
        public void add(int i){
            if(index < size){
                tempArray[index] = i;
                index++;
            }
            else{
                System.out.println("Array is full");
            }
        }
     
        public static void main(String[] args) {
            int[] array1 = {3,2,4,5,1};
     
            MyArray test1 = new MyArray();
     
            for(int i= 0; i < array1.length; i++){
                System.out.println(array1[i]);
                test1.add(array1[i]);
            }
        }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: ArrayIndexOutOfBounds

    What do you suppose size = ? when tempArray[] is initialized?

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    3sbwya (November 10th, 2013)

  4. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: ArrayIndexOutOfBounds

    Oh wow, I was assuming the array was getting initialized in the constructor but it wasn't. Stupid mistake. Fixed it and it works fine. Thanks

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: ArrayIndexOutOfBounds

    Glad to help.

Similar Threads

  1. ArrayIndexOutOfBounds Help
    By connorlm3 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2013, 12:36 AM
  2. I need help with ArrayIndexOutOfBounds exception!
    By jameschristian in forum Exceptions
    Replies: 6
    Last Post: November 3rd, 2012, 12:32 AM