0

为类分配做代码,我在这些行上得到空指针异常(在我的代码中加粗和斜体):我不明白这些行是如何指向一个空白点的,就像第一个是我的代码试图为某个座位设置名称,但显然我不能,因为它为空,即使我将我设置为 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) {
    }
}
4

3 回答 3

4

您的候补名单数组包含空元素。您必须先初始化元素,然后再尝试使用它们。

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 不应该是Passenger 的子类。这是没有意义的,因为候补名单的概念不是乘客概念的特殊情况。换句话说,它不满足继承的“is-a”标准。相反,它满足“有”的标准——等候名单上有乘客,而不是乘客。因此,请改用组合。
  • 接下来,WaitingList 类不应创建和填充 WaitingLists 数组,因为这将导致无限递归,每个新的 WaitingList 对象创建 10 个新的 WaitingList 对象,每个新的 WaitingList 对象将再创建 10 个 WaitingList 对象,每个对象将......你看到了吗问题?相反,WaitingList 类应该可能不继承自其他类,并且应该包含一个乘客对象数组(或 ArrayList),而不是 WaitingList 对象。

您的代码有很多问题,您可能希望重新启动此项目。

于 2013-07-30T04:55:40.077 回答
3

在此处输入图像描述


嘿,你声明array of references 了你需要做的是在使用它们之前初始化那些空引用

for(int i=0; i< waitlist.length;++i){
    waitlist[i] = new waitlist();
}

然后调用你的函数

于 2013-07-30T04:57:08.807 回答
-2

用它替换您的乘客等级代码。玩得开心 :)

 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());
        }
于 2013-07-30T05:03:18.140 回答