タイプ Seat の 2D 配列を定義し、メソッドを使用して座席要素をランダムに選択し、それを「予約済み」として設定するメイン クラスがあります。
ただし、複数の座席要素をランダムに選択する必要がある場合 (ランダムに選択する必要がある場合もあります)、次のエラーが返されます。
"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 の CreateTheater メソッド:
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;
}