4 Replies Latest reply: Aug 22, 2008 9:08 AM by 807589 RSS

    java.lang.ArrayIndexOutOfBoundsException

    807589
      Hi i am a first year student doing a project for uni, i am getting the java.lang.ArrayIndexOutOfBoundsException: 2 error in this bit of code and i cant figure out why. could someone please help?
      public class booking {
           public static void main(String[] args){
                int date, lengthOfHire, noOfPeople, totalCost;
                String typeOfBike;
                
                date = getDate();
                lengthOfHire = hireLength();
                noOfPeople = numPeople();
                String[] name = new String[noOfPeople];
                String[] bikeType = new String[noOfPeople];
                
                getNameAndBike(noOfPeople, name, bikeType);
           
                mountainBike[] bikeArray = new mountainBike [noOfPeople];
                
                for(int j = 0; j <= bikeArray.length; j++){
                     bikeArray[j] = new mountainBike();
                }
                for(
                     int i = 0; i <= bikeArray.length; i++){
                          if (bikeType[i] == "mountainbike"){
                               bikeArray.setCost(25);
                               bikeArray[i].setFrameSize("Large");
                               bikeArray[i].setHelmet(true);
                     }
                }
                
                System.out.println(name[0] + " " + " cost = " + bikeArray[0].getCost());
                System.out.println(" framesize = " + bikeArray[0].getFrameSize());
                }
      the error seems to be coming for this bit of code :
      for(int j = 0; j <= bikeArray.length; j++){
                     bikeArray[j] = new mountainBike();
                }
      the last couple of lines are just a test to help me see what my variables hold