0

我有一个主类,我在其中定义了一个 Seat 类型的二维数组,然后使用一种方法随机选择一个座位元素并将其设置为“已预订”

但是,如果它必须随机选择超过 1 个(有时即使它只需要随机选择一个)座位元素,它会返回此错误:

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

我不知道我做错了什么,可能与我的随机生成方法有关,无论如何,任何帮助将不胜感激。

主类:

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);

                        }
                    }

在我的座位类别中随机选择座位的方法:

 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;
}

172号线和173号线座位

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

173: book.bookSeat(true);}

Seat 类中的 CreateTheatre 方法:

 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;
            }
4

3 回答 3

1

如果您检查了CreateTheatre逻辑,则内部的值cinema可能为空。通过一个循环将默认值放入数组中。

以行为例,0您只填充从0to 到4然后19to 的值23。除此之外,我看不到您填写值。

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

更新:如果不想放空座位,那么您可以在调用之前添加空检查book.bookSeat(true)

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

您可以使用 打印多维数组Arrays.deepToString(cinema)

正如在其他线程使用中指出的那样book = x[rand.nextInt(12)][rand.nextInt(23)]

于 2012-10-29T18:03:02.880 回答
1

我不确定这是否是您收到错误的原因,我不知道这是来自哪条线路,但现在这是错误的:

你来做这件事:

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

然后你这样做:

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

这相当于:

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

当我假设你真正想要做的是:

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

考虑一下:

Seat[] y = x[rand.nextInt(x.length)];
book = y[rand.nextInt(y.length)];
于 2012-10-29T18:04:45.117 回答
1

我认为NullPointerException异常的根本原因在于CreateTheatre方法。请确保座位数组的所有行和列(即 12*23)都用SeatObject 填充。

一些观察:

  1. 这个循环似乎没有做任何事情,因为col它被初始化7并与<4.

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

    for (int row = 3; row < 5; row++) {
        for (int col = 14; col < 19; 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");
        }
     }
    

最后,有几个席位未初始化,如下所示,这是NullPointerExceptionbookSeat()方法中使用时造成的。

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

如果您想初始化所有座位,那么我认为您可能需要更新CreateTheatre以下循环:

       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");;
            }
        }

另外,由于您的行和列不一样(Seat[12][23]--> 12 行和 23 列),我认为您想在bookSeat()方法中更改此语句:

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

用作x[0].length列长度,如下所示:

      book = x[rand.nextInt(x.length)][rand.nextInt(x[0].length)];
于 2012-10-29T18:09:43.500 回答