Java Null Pointer创建新的类实例时的异常

时间:2014-04-22 06:41:32

标签: java nullpointerexception

我正处于计算机科学的第一年,我正忙着参与一个项目。我获得了通过数组创建房间号列表的代码,我将创建一种方法,允许用户选择房间,输入他们的信息,并保留房间。

我的问题似乎在于创建实际保留房间的方法......具体如下......

 public void bookRoom (Room[] room, int numberOfRooms)
{

    //Get settings for room (smoking, occupied, guest name/number)

    displayRoomsInfo(room, numberOfRooms);

        Room room1 = new Room(false, false, null, null, numberOfRooms);



    //user input,store each value to temp variables

    Scanner scan=new Scanner(System.in);
    System.out.println("Please choose a room");

    int guestRoomNum=scan.nextInt();
    room1.setRoomNumber(guestRoomNum);

    //set room[i] occupied = true;

    room1.setOccupied(true);

    //set room[i] guestname to name

    System.out.println("Enter Guest Name");
    String dickshit = scan.next();
    String[] nameList = {dickshit};
    room1.setGuestName(nameList);



    //set room[i] guestnumber to phone

    System.out.println("Enter Guest Phone Number");
    String phoneInput = scan.next();
    room1.setGuestPhone(phoneInput);
    }

我一直在

上获得空指针异常
Room room1 = new Room(false, false, null, null, numberOfRooms);

我实际上创建了我提供的自定义类实例的行。关于为什么或如何防止这种情况,我没有一个奇怪的线索,因为那些看起来是默认的构造函数值。

Room类代码如下:

public class Room {

private boolean smoking;
private boolean occupied;
private String[] guestName=new String[4];
private String guestPhone;
private int roomNumber;

public Room (boolean smoking,boolean occupied,String[] guestName, String guestPhone,int roomNumber)
{
    this.smoking=smoking;
    this.occupied=occupied;
    for (int i=0;i<4;i++)
        this.guestName[i]=guestName[i];
    this.guestPhone=guestPhone;
    this.roomNumber=roomNumber;

}
public void setGuestName(String[] guestName)
{
    for (int i=0;i<4;i++)
        this.guestName[i]=guestName[i];
}


public String[] getGuestName()
{
    String[] tempGuestName=new String[4];
    for (int i=0;i<4;i++)
        tempGuestName[i]=this.guestName[i];
    return tempGuestName;
}

public void setOccupied(boolean isFull)
{
    this.occupied=isFull;
}

public boolean getOccupied()
{
    return occupied;
}


public void setSmoking(boolean canSmoke)
{
    this.smoking=canSmoke;
}

public boolean getSmoking()
{
    return smoking;
}


public void setGuestPhone(String phoneNumber)
{
    this.guestPhone=phoneNumber;
}

public String getGuestPhone()
{
    return guestPhone;
}


public void setRoomNumber(int roomNum)
{
    this.roomNumber=roomNum;
}

public int getRoomNumber()
{
    return roomNumber;
}

}

5 个答案:

答案 0 :(得分:0)

那是因为在你的Room()构造函数中,你正在这样做。

for (int i=0;i<4;i++)
    this.guestName[i]=guestName[i];

但是你传递null guestName作为参数,因为guestName数组为null而引发NPE。

如果要传递null,请从构造函数中删除for循环。如果没有,请将空白String[]传递给构造函数。

Room room1 = new Room(false, false, new String[4], null, numberOfRooms);

请注意,即使您执行上述操作,String[]中的每个元素仍然只是null引用,您需要稍后在程序中显式初始化它们。

答案 1 :(得分:0)

guestName在

中为空
 Room room1 = new Room(false, false, null, null, numberOfRooms);

但是你的构造函数要求它不能为空

for (int i=0;i<4;i++)
    this.guestName[i]=guestName[i];

您应该在构造函数中添加一个测试:

if(guestName != null)

或在调用构造函数时传递有效数组

答案 2 :(得分:0)

为什么不获取用户输入,然后为它腾出空间? 用户输入数据后(并将其保存到变量中)

Room room1 = new Room(false, true, nameList, phoneInput, roomNumber);

答案 3 :(得分:0)

在你的for循环中

for (int i=0;i<4;i++)
    this.guestName[i]=guestName[i];

Argument guestName为null。由于您没有提供任何价值:

    Room room1 = new Room(false, false, null, null, numberOfRooms);

见第3段。

答案 4 :(得分:0)

您正在尝试在构造函数中访问访客名称,该访问权限为null。请注意,您不应对传递/分配/转为Null的值执行任何操作。