Java 尝试从数组中随机选择值时出错
我有一个main类,在这个类中我定义了一个Seat类型的2d数组,然后使用一个方法随机选择一个Seat元素并将其设置为“booked” 但是,如果必须随机选择1个以上(有时甚至只需要随机选择一个)座椅元素,则返回此错误:Java 尝试从数组中随机选择值时出错,java,arrays,random,Java,Arrays,Random,我有一个main类,在这个类中我定义了一个Seat类型的2d数组,然后使用一个方法随机选择一个Seat元素并将其设置为“booked” 但是,如果必须随机选择1个以上(有时甚至只需要随机选择一个)座椅元素,则返回此错误: "Exception in thread "main" java.lang.NullPointerException at cinemasystem.Seat.bookSeat(Seat.java:173) at cinemasystem.CinemaSyste
"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);
}
}
在my seat类中随机选择座位的方法:
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;
}
如果选中
CreateTheatre
逻辑,则cinema
中的值可能为空。通过一个循环将默认值放入数组中
以行0
为例,您只需将值从0
填充到4
,然后从19
填充到23
。除此之外,我没有看到你填充值
for(int i = 0; i<cinema.length;i++)
for(int j=0; j<cinema[i].length;j++)
cinema[i][j] = new Seat("");
您可以使用
arrays.deepToString(电影院)
打印多维数组
正如在其他线程使用中指出的那样,如果选中
CreateTheatre
逻辑,cinema
中的值可能为空。通过一个循环将默认值放入数组中
以行0
为例,您只需将值从0
填充到4
,然后从19
填充到23
。除此之外,我没有看到你填充值
for(int i = 0; i<cinema.length;i++)
for(int j=0; j<cinema[i].length;j++)
cinema[i][j] = new Seat("");
您可以使用
arrays.deepToString(电影院)
打印多维数组
正如在其他线程使用
book=x[rand.nextInt(12)][rand.nextInt(23)]
中指出的,我不确定这是否是导致错误的原因,我不知道这是从哪一行来的,但现在这是错误的:
您可以这样做:
Seat[][] cinema = new Seat[12][23];
book = x[rand.nextInt(x.length)][rand.nextInt(x.length)];
然后你要这样做:
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)];
我不确定这是否是你出错的原因,我不知道这是从哪一行来的,但现在这是错误的: 您可以这样做:
Seat[][] cinema = new Seat[12][23];
book = x[rand.nextInt(x.length)][rand.nextInt(x.length)];
然后你要这样做:
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)];
我认为
NullPointerException
异常的根本原因是CreateTheatre
方法。请确保座椅阵列的所有行和列(即12*23)都填充了seat
对象
一些观察结果:
col
是用7
初始化的,并且与进行比较,我认为NullPointerException
异常的根本原因是CreateTheatre
方法。请确保座椅阵列的所有行和列(即12*23)都填充了seat
对象
一些观察结果:
这个循环似乎没有任何作用,因为col
是用7
初始化的,并且与进行比较。您能指出Seat.java的第173行吗?或者更好的方法是:在发布之前检查该行。您试图在173
行上使用null
的内容。我猜这是你分配给book
之后的一行。找出为什么是空的。提示:您从未将对象放在该索引位置。AmitD和Sticky的回答都涉及代码的重要问题。请您指出Seat.java的第173行好吗?或者更好的做法是:在发布之前检查该行。您试图在第173行173
上使用null
的内容。我猜这是你分配给book
之后的一行。找出为什么是空的。提示:您从未在该索引位置放置对象。AmitD和Sticky的答案都涉及代码的重要问题。啊,好吧,那么这将使它通过两个数组?此外,它现在似乎并没有抛出异常,输出是有意的。显然do{book=x[rand.nextInt(x.length)][rand.nextInt(x[0.length)];book.bookSeat(true);}while(book.isbook());}计数+++;}还书;}代码>需要更改为do{book=x[rand.nextInt(x.length)][rand.nextInt(x[0.length)];book.bookSeat(false);}而(book.isbooks(true));}计数+++;}还书;}
@user1719605请注意,异常取决于通过rand.nextInt
方法生成的随机数。请参考我的更新答案,并纠正CreateTheatre
方法中的问题。问题:如果bookSeat()
方法中的if(x!=null)
不允许选择null元素吗?@user1719605:检查数组本身是否为null。当您访问一些未初始化的seat并调用方法book.bookSeat(true)时,就会出现问题代码>在那上面。我在答案中添加了更多的细节。啊,正如我向其他人提到的,数组需要有一些不是座位的元素,因为第一行中只有8个座位对象。我使用multiple for循环的另一个原因是,一些席位需要不同的字符串值。啊,好的,那么这将使它通过两个数组?此外,它现在似乎并没有抛出异常,输出是有意的。显然do{book=x[rand.nextInt(x.length)][rand.nextInt(x[0.length)];book.bookSeat(true);}while(book.isbook());}计数+++;}还书;}代码>需要更改为do{book=x[rand.nextInt(x.length)][rand.nextInt(x[0.length)];book.bookSeat(false);}而(book.isbooks(true));}计数+++;}还书;}代码>@user1719605请注意,异常取决于生成的随机数