如何在Java中正确使用合成?

时间:2017-10-27 00:15:41

标签: java nullpointerexception composition

我对Java比较陌生,我试图在创建的Resident类中使用组合,该类扩展了另一个类Person。我得到一个指向这行代码的空指针异常错误:

equipment.setEquipment(hasInitialEquipment);

在我的常驻类构造函数中。我知道我一定做错了,但我真的不确定它是什么。任何帮助将不胜感激。

public class Driver1 {

public static void main(String[] args) {
   Resident a = new Resident("Bob Smith", 74, "Male", 317, 'C', false, false, "");

   a.writeOutput();
   System.out.println();
 } 
}

班级人员

public class Person {

    private String name;

    public Person()
    {
        name = "No name given";
    }

    public Person(String initialName)
    {
        name = initialName;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String newName)
    {
        name = newName;
    }

    public void writeOutput()
    {
        System.out.println(name);
    }
}

班级居民

public class Resident extends Person {

public static final int DEFAULT_AGE = 0;
public static final int DEFAULT_ROOM = 0;


private int age;
private String gender;
private int room;
private char building;
private boolean supervised;

private Equipment equipment;

public Resident() 
{
    super();
    age = DEFAULT_AGE;
    gender = "Undeclared";
    room = DEFAULT_ROOM;
    building = '0';
    supervised = false;

}

public Resident(String initialName, int initialAge, String initialGender, 
    int initialRoom, char initialBuilding, boolean needsSupervision,
    boolean hasInitialEquipment, String initialEquipmentType) 
{
    super(initialName);
    age = initialAge;
    gender = initialGender;
    room = initialRoom;
    building = initialBuilding;
    supervised = needsSupervision;
    equipment.setEquipment(hasInitialEquipment);
    equipment.setEquipmentType(initialEquipmentType);

}

public int getAge()
{
    return age;
}

public void setAge(int newAge)
{
    age = newAge;
}

public String getGender()
{
    return gender;
}

public void setGender(String newGender)
{
    gender = newGender;
}

public int getRoom()
{
    return room;
}

public void setRoom(int newRoom)
{
    room = newRoom;
}

public char getBuilding()
{
    return building;
}

public void setBuilding(char newBuilding)
{
    building = newBuilding;
}

public boolean getSupervised()
{
    return supervised;
}

public void setSupervised(boolean newSupervised)
{
    supervised = newSupervised;
}

public boolean getEquipment()
{
    return equipment.getEquipment();
}        

public String getEquipmentType() 
{
    return equipment.getEquipmentType();
}

@Override
public void writeOutput()
{
    System.out.println(getName());
    System.out.println("Age: " + age);
    System.out.println("Gender: " + gender);
    System.out.println("Room: " + room);
    System.out.println("Building: " + building);
    if(supervised){
        System.out.print("***Supervised Ambulation only***");
    if(getEquipment())
        System.out.println(getEquipmentType());
    }
 }
}

班级设备

public class Equipment {

private boolean equipment;
private String equipmentType;

public Equipment() 
{
    equipment = false;
    equipmentType = null;
}

public Equipment(boolean initialEquipment, String initialEquipmentType) 
{
    equipment = initialEquipment;
    equipmentType = initialEquipmentType;
}        

public boolean getEquipment()
{
    return equipment;
}

public void setEquipment(boolean newEquipment)
{
    equipment = newEquipment;
}

public String getEquipmentType() 
{
    return equipmentType;
}

public void setEquipmentType(String newEquipmentType)
{
    equipmentType = newEquipmentType;
}

public void WriteOutput() 
{
    if(equipment)
        System.out.println("Equipment: " + equipmentType);

 }
}

1 个答案:

答案 0 :(得分:1)

您在Resident

中声明了一个设备
private Equipment equipment;

你在初始化之前调用了一个方法。您必须在使用之前初始化此变量。在调用Resident上的方法之前,将以下内容放入equipment构造函数中。

equipment = new Equipment();

当您声明非基本变量时,此变量将保留null,直到您初始化它为止。这就是你获得NullPointerException的原因。