为类分配做代码,我在这些行上得到空指针异常(在我的代码中加粗和斜体):我不明白这些行是如何指向一个空白点的,就像第一个是我的代码试图为某个座位设置名称,但显然我不能,因为它为空,即使我将我设置为 0,名字设置为空字符串,等等。帮助!
public class Passenger {
String FirstName;
String LastName;
void getName() {
FirstName = " ";
LastName = " ";
}
void getName(String first, String last) {
FirstName = first;
LastName = last;
}
String getFirst() {
return FirstName;
}
String getLast() {
return LastName;
}
void setFirstName(String tempfirst) {
FirstName = tempfirst;
}
void setLastName(String templast) {
LastName = templast;
}
void printNames() {
System.out.printf("%s %s\n", this.getFirst(), this.getLast());
}
public static void main(String[] args) {
}
}
public class WaitingList extends Passenger {
Passenger[] waitlist = new WaitingList[10];
WaitingList(){
for(int i = 0; i<10;i++){
***waitlist[i].setFirstName("");***
waitlist[i].setLastName("");
}}
void printWaitingList() {
for (int j = 0; j < 10; j++)
waitlist[j].printNames();
}
void setPassenger(int i, Passenger tempPass) {
waitlist[i] = tempPass;
}
Passenger getPassenger(int i) {
return waitlist[i];
}
public static void main(String[] args) {
}
}
import java.io.Console;
import java.util.Scanner;
public class SeatingChart extends WaitingList {
Passenger[][] pass = new Passenger[10][4];
SeatingChart() {
for (int row = 0; row < 10; row++) {
for (int col = 0; col < 4; col++) {
pass[row][col] = new Passenger();
}
}
}
void printSeatingChart() {
for (int row = 0; row < 10; row++) {
for (int col = 0; col < 4; col++) {
System.out.print(pass[row][col].getFirst() + "\t");
System.out.print(pass[row][col].getLast() + "\n");
}
}
}
String getSeatChoicefromUser() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter preferred seat (row, column): ");
String seat = scanner.next();
return seat;
}
void setPassenger(int row, int col, Passenger temppass) {
pass[row][col] = temppass;
}
Passenger getPassenger(int row, int col) {
return pass[row][col];
}
Passenger search(String first, String last) {
Passenger search = new Passenger();
for (int row = 0; row < 10; row++) {
for (int col = 0; col < 4; col++) {
String tempfirst = pass[row][col].getFirst();
String templast = pass[row][col].getLast();
if (tempfirst.equals(first)) {
if (templast.equals(last)) {
search = pass[row][col];
removePassenger(row, col);
}
} else {
search = null;
System.out.print("\nPassenger not found\n");
}
}
}
return search;
}
void removePassenger(int row, int col) {
pass[row][col].setFirstName(null);
pass[row][col].setLastName(null);
}
Passenger reconcileWithWaitingList() {
int i = 0;
Passenger pass = new Passenger();
String tempfirst = waitlist[i].getFirst();
String templast = waitlist[i].getLast();
if (tempfirst.equals(null)) {
if (templast.equals(null))
System.out.print("\nWaiting list is empty\n");
} else {
pass = waitlist[i];
for (i = 0; i < 10; i++)
waitlist[i] = waitlist[i + 1];
}
return pass;
}
public void addPassengerToWaitingList(int wrow, int wcol) {
Passenger addPass = new Passenger();
Scanner sc = new Scanner(System.in);
System.out.print("Enter first name: ");
String tempfirst = sc.next();
System.out.print("Enter last name: ");
String templast = sc.next();
addPass = search(tempfirst, templast);
if (addPass == null) {
String tempfirst1 = pass[wrow][wcol].getFirst();
String templast1 = pass[wrow][wcol].getLast();
if (tempfirst1.equals(null)) {
if (templast1.equals(null)) {
pass[wrow][wcol].setFirstName(tempfirst1);
pass[wrow][wcol].setLastName(templast1);
}
}
} else
System.out.print("Passenger already present!");
}
public static void main(String[] args) {
}
}