1 2 Previous Next 20 Replies Latest reply on Feb 15, 2009 11:46 PM by 800423

# Array Index Out of Bounds Expeception

Hi Guys,

I'm having trouble with a method which takes an array of objects and increases its length by one. I'm doing this by calling a method that looks at the length of the array, makes a new array with the length + 1, copies all the objects from the old to the new array. It then returns the new array overwriting the new one. This works find and if I system.out.println the length of the new array its value is 7 which is 1 more than before. The problem im having is that when i try and add stuff to an object in the array I get the index our of bound exception: 7.

The code is as follows:
``````void newcustomer(){

int len = 0;
int newcustomerID = 0;

len = customerarray.length;

System.out.println();
System.out.println("Enter Customer ID: ");

customerarray[len].setcustomerID(newcustomerID);

}

public Customer[] addcustomer ( Customer[] oldarray ){

int len = 0;

len = customerarray.length;
len++;

Customer newcustomerArray[] = new Customer[len];

for (int i=0; i<oldarray.length; i++){
oldarray[i] = newcustomerArray;
}

return(newcustomerArray);
}Many Thanks!!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ``````
• ###### 1. Re: Array Index Out of Bounds Expeception
``````len = customerarray.length;
customerarray[len].setcustomerID(newcustomerID);``````
This is not going to work, the last element is (of course) len - 1
• ###### 2. Re: Array Index Out of Bounds Expeception
So the length takes into account the index starting at 0, so even though it says 7 it actualy means 6 because of 0?
• ###### 3. Re: Array Index Out of Bounds Expeception
Here
``````len = customerarray.length;
//...
customerarray[len].setcustomerID(newcustomerID);``````
You know that an array goes from 0 to length - 1, right?
so if you do this, it's doomed to fail:
``````int[] intArray = {1, 2, 3, 4};
int len = intArray.length;
System.out.println(intArray[len]); // !!! no !!!``````
• ###### 4. Re: Array Index Out of Bounds Expeception
so even though it says 7 it actualy means 6
Not exactly. The length = 7 (7 elements), but the last element is on position 6 (because the first one is on 0). There is a difference between the length and the last position!!
• ###### 5. Re: Array Index Out of Bounds Expeception
I have adjusted the code so it takes this into account:
``````len = customerarray.length;
newposition = len--;

System.out.println();
System.out.println("Enter Customer ID: ");

customerarray[newposition].setcustomerID(newcustomerID);``````
newposition is length - 1 but I am still getting the same problem?

Thanks for the help guys!
• ###### 6. Re: Array Index Out of Bounds Expeception
read up on the differences between
``--len and len--``
• ###### 7. Re: Array Index Out of Bounds Expeception
Yes
• ###### 8. Re: Array Index Out of Bounds Expeception
newposition = len--;

Try to understand the difference between
len-- and --len
The first one assigns the value of len to newpos, and then decreases len with one
The second one first decreases the value of len, and then assigns the new value of len to newpos.

newpos = customerarray.length - 1;
• ###### 9. Re: Array Index Out of Bounds Expeception
I have changed it as you suggested and can see where I was going wrong. The code now is:
``````len = studentarray.length;
newposition = studentarray.length - 1;``````
I'm now getting a NullPointerException error?

Does anyone know why this is?

Thanks for all the help!
• ###### 10. Re: Array Index Out of Bounds Expeception
OK, I have to ask you to just stop. Stop right now.

Please rethink your method of debugging here, for if you continue with this protocol:
* find an error
* immediately post the error on the forum.

you will never ever learn to think for yourself. You need to do some of your own thinking and work here. Please. It's starting to get a bit ridiculous here.
• ###### 11. Re: Array Index Out of Bounds Expeception
We where talking about a CUSTOMERarray all the time, and now you have a STUDENTarray???
• ###### 12. Re: Array Index Out of Bounds Expeception
I'd changed the name of it in the latest revision of the program.
• ###### 13. Re: Array Index Out of Bounds Expeception
Blarg275 wrote:
I'm now getting a NullPointerException error?

Does anyone know why this is?
Yes.
• ###### 14. Re: Array Index Out of Bounds Expeception
lol :)
1 2 Previous Next