尝试从数组中随机选择值时出错

时间:2012-10-29 17:56:50

标签: java arrays random

我有一个主类,我定义了一个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;
}

Seat Class的第172和173行

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

173:book.bookSeat(true);}

在座位类中的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;
            }

3 个答案:

答案 0 :(得分:1)

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

以行0为例,您只需将值从0填充到4,然后填充1923。除此之外,我没有看到你填补价值。

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

答案 1 :(得分: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)];

答案 2 :(得分:1)

我认为NullPointerException异常的根本原因在于CreateTheatre方法。请确保座椅阵列的所有行和列(即12 * 23)都填充了Seat对象。

一些观察结果:

  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");
        }
     }
    
  3. 最后,几个席位未初始化,如下所示,在NullPointerException方法中使用时会导致bookSeat()

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

    此外,,因为您的行和列不相同(座位[12] [23] - > 12行和23列),我想您要在{更改此声明{1}}方法:

    bookSeat()

    使用 book = x[rand.nextInt(x.length)][rand.nextInt(x.length)]; 作为列标题,如下所示:

    x[0].length