1 2 Previous Next 20 Replies Latest reply: Feb 15, 2009 4:56 PM by 843785 RSS

    Array Index Out of Bounds Expeception

    800423
      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;
           
           customerarray = addcustomer(customerarray);
           
           len = customerarray.length;
           
           System.out.println();     
           System.out.println("Enter Customer ID: ");     
           
           newcustomerID = in.readInteger();
           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 2 Previous Next