为类分配做代码,并在这些行上获得空指针异常(在我的代码中用粗体和斜体表示):
我不明白这些行是如何指向空白的,例如第一个是我的代码试图为某个座位设置名称,但是显然我不能,因为它为空,即使我将其设置为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) {
}
}
最佳答案
您的等待列表数组包含空元素。您必须先初始化元素,然后再尝试使用它们。
Passenger[] waitlist = new WaitingList[10];
WaitingList(){
for(int i = 0; i<10;i++){
waitlist[i] = new WaitingList(); // *** first create an element
waitlist[i].setFirstName(""); // *** before using it.
waitlist[i].setLastName("");
}
}
想一想类似于停车场的对象阵列。当您创建与创建批次相同的数组时,尽管批次拥有很多空间,但最初批次是空的。在您第一次将汽车放入卡车之前,您不能驾驶任何汽车。同样,您不能使用任何数组项,除非它们引用诚实对象。
编辑
关于您的评论,
在同一行上给了我一个堆栈溢出错误
这是一个全新的错误-您那里有一些递归代码。该问题是由于以下代码引起的:
public class WaitingList extends Passenger {
Passenger[] waitlist = new WaitingList[10];
首先,WaitingList不应是旅客的子类。这没有意义,因为候补名单的概念不是乘客概念的特殊情况。换句话说,它不满足继承的“是”标准。相反,它满足“具有”标准-等候名单上有乘客,而不是-乘客。因此,请改用合成。
接下来,WaitingList类不应创建和填充WaitingLists数组,因为这将导致无限递归,每个新的WaitingList对象将创建10个新的WaitingList对象,每个对象将创建10个以上WaitingList对象,每个对象将...问题?相反,WaitingList类可能不应该继承自其他任何类,并应保存一个Passenger对象而不是WaitingList对象的数组(或ArrayList)。
您的代码有很多问题,您可能希望重新启动该项目。