为类赋值执行代码,并在这些行上获取空指针异常(在我的代码中以粗体和斜体显示): 我不明白这些线是如何指向一个空白点,就像第一个是我的代码试图为某个座位设置名称,但显然我不能因为它是null,即使我已经设置为0,名字设置为空字符串,等等helP!
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) {
}
}
答案 0 :(得分:4)
您的waitlist数组包含null元素。在尝试使用它们之前,必须首先初始化元素。
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];
您的代码存在很多问题,您可能希望重新启动此项目。
答案 1 :(得分:3)
嘿,你宣布array of references
您需要做的是在使用它们之前初始化这些空引用
for(int i=0; i< waitlist.length;++i){
waitlist[i] = new waitlist();
}
然后调用你的函数
答案 2 :(得分:-2)
用它替换您的Passenger类代码。玩得开心:)
public class Passenger {
String FirstName = "";
String LastName = "";
void getName(){
FirstName="";
LastName="";}
void getName(String first, String last){
if(first != null)
{
FirstName=first;
}
else
{
FirstName = "";
}
if(last != null)
{
LastName=last;
}
else
{
LastName = "";
}
}
String getFirst(){
return FirstName;}
String getLast(){
return LastName;}
void setFirstName(String tempfirst){
if(tempfirst == null)
{
FirstName = "";
}
else
{
FirstName=tempfirst;
}
}
void setLastName(String templast){
if(templast == null)
{
LastName="";
}
else
{
LastName=templast;
}
}
void printNames(){
System.out.printf("%s %s\n", this.getFirst(), this.getLast());
}