简体   繁体   中英

Getting an error when trying to randomly select a value from an array

I have main class where I define a 2d array of type Seat and then use a method to randomly select a seat element and set it as "booked"

However if it has to randomly select more than 1 (Sometimes even if it only needs to randomly select one) seat element it returns this error:

"Exception in thread "main" java.lang.NullPointerException
    at cinemasystem.Seat.bookSeat(Seat.java:173)
    at cinemasystem.CinemaSystem.main(CinemaSystem.java:70)
Java Result: 1"

I have no clue what I am doing wrong, probably something with my random generating method, anyhow any help would be appreciated.

Main class:

public static void main(String[] args) {


        Seat cinemaactual = new Seat("Cinema");
        Seat[][] cinema = new Seat[12][23];
        Seat bookedSeat = new Seat("");

        Ticket ticket = new Ticket();
        Scanner scan = new Scanner(System.in);
        String answer, contiune, list;
        cinemaactual.CreateTheatre(cinema);
        int number, check = 0, category, id;

        do {

            System.out.print("Welcome to the Theatre Booking System. (QUIT to exit)"
                    + "\nWould you like to purchase tickets or list available seats?"
                    + "(/Purchase/List/Help)");
            answer = scan.nextLine();

            if (answer.equalsIgnoreCase("purchase")) {
                do {

                    System.out.println("Please choose the cateogry of ticket "
                            + "you would like, followed by who the ticket is for"
                            + "and the amount required. (separated by a space)\n"
                            + "1. Gold\n2. Silver\n3. Bronze\n\n1. Adult\n2."
                            + " Child\n3. Concession");
                    category = scan.nextInt();
                    check = category;
                    id = scan.nextInt();
                    number = scan.nextInt();
                    if (category == 1 || category == 2 || category == 3 && id == 1 || id == 2 || id == 3) {

                        ticket.SetType(category);
                        if (category == 1) {
                            ticket.SetName("Gold");
                        } else if (category == 2) {
                            ticket.SetName("Siler");
                        } else {
                            ticket.SetName("Bronze");
                        }
                        ticket.SetNumber(number);
                        ticket.SetID(id);
                        if (id == 1) {
                            ticket.SetCategory("Adult");
                        } else if (id == 2) {
                            ticket.SetCategory("Child");
                        } else {
                            ticket.SetCategory("Bronze");
                        }
                        System.out.print("You have selected "
                                + ticket.GetNumber() + " " + ticket.GetName()
                                + " ticket(s) at the " + ticket.GetCategory() + " price.");
                        System.out.println();
                        ticket.BuyTicket(category, id);
                        bookedSeat = Seat.bookSeat(cinema, number);


                    } else {

                        System.out.print("Sorry, incorrect input, please enter an apropriate value.");
                        check = scan.nextInt();
                    }
                } while (check == 0 || check > 3);

                do {
                    System.out.print("Would you like to perchase more tickets? (Yes/No)");
                    contiune = scan.nextLine();


                    if (contiune.equalsIgnoreCase("Yes")) {
                        System.out.print("Would you like to list available seats? (Yes/No) (A/G/S/B)");
                        list = scan.nextLine();
                        cinemaactual.DisplayTheatre(cinema);
                        if (list.equalsIgnoreCase("Yes")) {
                            cinemaactual.DisplayTheatre(cinema);

                        }
                    }

The method that randomly selects a seat, in my seat class:

 public static Seat bookSeat(Seat[][] x, int number){
        int count = 0;
        Seat book = new Seat("");
        Random rand = new Random();
        while(count < number){
    if (x != null) {

        do {
        book = x[rand.nextInt(x.length)][rand.nextInt(x.length)];
        book.bookSeat(true);}

        while (book.isBooked());
    }   count++; }
    return book;
}

Line 172 and 173 of Seat Class

172: book = x[rand.nextInt(x.length)][rand.nextInt(x.length)];

173: book.bookSeat(true);}

CreateTheatre Method in class Seat:

 public Seat[][] CreateTheatre(Seat[][] x) {

        for (int row = 0; row < 8; row++) {
            for (int col = 0; col < 4; col++) {
                x[row][col] = new Seat("B");
            }
        }
        for (int row = 8; row < 12; row++) {
            for (int col = 0; col < 4; col++) {
                x[row][col] = new Seat("S");
            }
        }

        for (int row = 0; row < 8; row++) {
            for (int col = 19; col < 23; col++) {
                x[row][col] = new Seat("B");
            }
        }
        for (int row = 8; row < 12; row++) {
            for (int col = 19; col < 23; col++) {
                x[row][col] = new Seat("S");
            }
        }
        for (int row = 3; row < 5; row++) {
            for (int col = 4; col < 9; col++) {
                x[row][col] = new Seat("B");
            }
        }
        for (int row = 3; row < 5; row++) {
            for (int col = 14; col < 19; col++) {
                x[row][col] = new Seat("S");
            }
        }
        for (int row = 9; row < 12; row++) {
            for (int col = 7; col < 4; col++) {
                x[row][col] = new Seat("S");
            }
        }
        for (int row = 3; row < 5; row++) {
            for (int col = 14; col < 20; col++) {
                x[row][col] = new Seat("B");
            }
        }

        for (int row = 5; row < 9; row++) {
            for (int col = 4; col < 9; col++) {
                x[row][col] = new Seat("S");
            }
        }

        for (int row = 5; row < 9; row++) {
            for (int col = 14; col < 20; col++) {
                x[row][col] = new Seat("S");
            }
        }
        for (int row = 6; row < 9; row++) {
            for (int col = 9; col < 14; col++) {
                x[row][col] = new Seat("G");
            }
        }
        for (int row = 9; row < 12; row++) {
            for (int col = 7; col < 16; col++) {
                x[row][col] = new Seat("G");
            }
        }

        for (int row = 9; row < 12; row++){
            for (int col = 4; col < 7; col++){
                x[row][col] = new Seat("S");
            }
        }

        for (int row = 9; row < 12; row++){
            for (int col = 16; col < 19; col++){
                x[row][col] = new Seat("S");
            }
        }
        return x;
            }

If you checked CreateTheatre logic there are possibilities that values inside cinema can be null. Put default values inside array by having one loop.

Take a example of row 0 you only fill values from 0 to 4 and then 19 to 23 . Apart from that I do not see you filling values.

for(int i = 0; i<cinema.length;i++)
    for(int j=0; j<cinema[i].length;j++)
      cinema[i][j] = new Seat("");

Update: If don't want to put empty seats then you can just add null check before calling book.bookSeat(true)

if(book != null){
 book.bookSeat(true);
}

You can print multidimentional arrays using Arrays.deepToString(cinema) .

As pointed out in other thread use book = x[rand.nextInt(12)][rand.nextInt(23)]

I'm not sure if this is why you're getting the error, I don't know what line that's coming from, but this is wrong right now:

You do this:

Seat[][] cinema = new Seat[12][23];

Then you do this:

book = x[rand.nextInt(x.length)][rand.nextInt(x.length)];

Which is equivalent to:

book = x[rand.nextInt(12)][rand.nextInt(12)]

When I'm assuming what you actually want to do is:

book = x[rand.nextInt(12)][rand.nextInt(23)]

Consider this:

Seat[] y = x[rand.nextInt(x.length)];
book = y[rand.nextInt(y.length)];

I think the root cause of NullPointerException exception is in CreateTheatre method. Please make sure all rows and columns (ie 12*23) of the seat array is filled with Seat Object.

Some observations:

  1. This loop doesn't seem to do anything as col is initialized with 7 and getting compared against <4 .

     for (int row = 9; row < 12; row++) { for (int col = 7; col < 4; col++) { x[row][col] = new Seat("S"); } } 
  2. This loops

     for (int row = 3; row < 5; row++) { for (int col = 14; col < 19; col++) { x[row][col] = new Seat("S"); } } 

    seems to be overlapping with this loop as its using same rows but overlapping columns.

     for (int row = 3; row < 5; row++) { for (int col = 14; col < 20; col++) { x[row][col] = new Seat("B"); } } 

In the end, several seats are left uninitialized as below, which is causing NullPointerException when being used in bookSeat() method.

B, B, B, B, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, B, B, B, B
B, B, B, B, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, B, B, B, B
B, B, B, B, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, B, B, B, B
B, B, B, B, B,    B,    B,    B,    B,    null, null, null, null, null, B,    B,    B,    B,    B,    B, B, B, B
B, B, B, B, B,    B,    B,    B,    B,    null, null, null, null, null, B,    B,    B,    B,    B,    B, B, B, B
B, B, B, B, S,    S,    S,    S,    S,    null, null, null, null, null, S,    S,    S,    S,    S,    S, B, B, B
B, B, B, B, S,    S,    S,    S,    S,    G,    G,    G,    G,    G,    S,    S,    S,    S,    S,    S, B, B, B
B, B, B, B, S,    S,    S,    S,    S,    G,    G,    G,    G,    G,    S,    S,    S,    S,    S,    S, B, B, B
S, S, S, S, S,    S,    S,    S,    S,    G,    G,    G,    G,    G,    S,    S,    S,    S,    S,    S, S, S, S
S, S, S, S, S,    S,    S,    S,    S,    G,    G,    G,    G,    G,    S,    S,    S,    S,    S,    S, S, S, S
S, S, S, S, S,    S,    S,    S,    S,    G,    G,    G,    G,    G,    S,    S,    S,    S,    S,    S, S, S, S
S, S, S, S, S,    S,    S,    S,    S,    G,    G,    G,    G,    G,    S,    S,    S,    S,    S,    S, S, S, S

If you want to initialize all the seats then I think you may want to update loops in your CreateTheatre as below:

       for (int row = 0; row < 8; row++) {
            for (int col = 0; col < 4; col++) {
                x[row][col] = new Seat("B");;
            }
        }
        for (int row = 8; row < 12; row++) {
            for (int col = 0; col < 4; col++) {
                x[row][col] = new Seat("S");;
            }
        }

        for (int row = 0; row < 8; row++) {
            for (int col = 19; col < 23; col++) {
                x[row][col] = new Seat("B");;
            }
        }

        for (int row = 0; row < 3; row++) {
            for (int col = 4; col < 20; col++) {
                x[row][col] = new Seat("G");;
            }
        }

        for (int row = 8; row < 12; row++) {
            for (int col = 19; col < 23; col++) {
                x[row][col] = new Seat("S");;
            }
        }
        for (int row = 3; row < 5; row++) {
            for (int col = 4; col < 9; col++) {
                x[row][col] = new Seat("B");;
            }
        }
        for (int row = 3; row < 5; row++) {
            for (int col = 8; col < 20; col++) {
                x[row][col] = new Seat("S");;
            }
        }
        for (int row = 9; row < 12; row++) {
            for (int col = 7; col < 14; col++) {
                x[row][col] = new Seat("S");;
            }
        }
        for (int row = 9; row < 12; row++) {
            for (int col = 14; col < 20; col++) {
                x[row][col] = new Seat("B");;
            }
        }

        for (int row = 5; row < 9; row++) {
            for (int col = 4; col < 9; col++) {
                x[row][col] = new Seat("S");;
            }
        }

        for (int row = 5; row < 9; row++) {
            for (int col = 14; col < 20; col++) {
                x[row][col] = new Seat("S");;
            }
        }
        for (int row = 5; row < 9; row++) {
            for (int col = 9; col < 14; col++) {
                x[row][col] = new Seat("G");;
            }
        }
        for (int row = 9; row < 12; row++) {
            for (int col = 7; col < 16; col++) {
                x[row][col] = new Seat("G");;
            }
        }

        for (int row = 9; row < 12; row++){
            for (int col = 4; col < 7; col++){
                x[row][col] = new Seat("S");;
            }
        }

        for (int row = 9; row < 12; row++){
            for (int col = 16; col < 19; col++){
                x[row][col] = new Seat("S");;
            }
        }

In addition, since your rows and columns are not same(Seat[12][23]--> 12 rows and 23 columns), I think you want to change this statement in bookSeat() method:

     book = x[rand.nextInt(x.length)][rand.nextInt(x.length)];

to use x[0].length as column legth as below:

      book = x[rand.nextInt(x.length)][rand.nextInt(x[0].length)];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM