0

public class BoeingSeatMap extends SeatMap{

public BoeingSeatMap() {
    this.seats= new Seat[nRows][nColumns] ;
    this.nColumns=7;
    this.nRows=10;
    this.nFirstClassRows=4;
    initialiseSeatMap();
}
protected void initialiseSeatMap() {
    String str="ABCDEFG";

    for (int i=0;i<nRows;i++) {
        seats[i][0].getSeatPostion().setRow(i+1);
        seats[i][0].getSeatPostion().setColumn(str.charAt(0));
        seats[i][0].setSeatType(SeatType.WINDOW);

        seats[i][nColumns-1].getSeatPostion().setRow(i+1);
        seats[i][nColumns-1].getSeatPostion().setColumn(str.charAt(6));
        seats[i][nColumns-1].setSeatType(SeatType.WINDOW);
        if (i<=4) {
            seats[i][0].setFirstClass(true);
            seats[i][nColumns-1].setFirstClass(true);
        }
    }

Just wondering why this would return a ArrayIndexOutOfBoundsException when I am trying to store into a part of the 2d array that shouldnt be out of bounds eg [1][6]

2 Answers 2

2

You didn't include the entire class, but from what I can see you are instantiating "nColumns" and "nRows" afters "seats". Try modifying the constructor:

public BoeingSeatMap() {
    this.nColumns=7;
    this.nRows=10;
    this.seats= new Seat[nRows][nColumns] ;
    this.nFirstClassRows=4;
    initialiseSeatMap();
}
Sign up to request clarification or add additional context in comments.

Comments

0

You must initialize nColumns and nRows fields before you create seats array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.