Java 为什么即使我将o.pet设置为一个值,它仍然为null?
我正在一个救援中心做一个apcs项目,在那里人们可以领养动物等等。我遇到的问题是,在我的UI中,当我尝试列出所选所有者的宠物时,出现了一个异常,表示它无法读取o.pet,因为它是空的。我想知道为什么它是空的,即使我在owner-adopt函数中将a的owner设置为Java 为什么即使我将o.pet设置为一个值,它仍然为null?,java,exception,null,project,computer-science,Java,Exception,Null,Project,Computer Science,我正在一个救援中心做一个apcs项目,在那里人们可以领养动物等等。我遇到的问题是,在我的UI中,当我尝试列出所选所有者的宠物时,出现了一个异常,表示它无法读取o.pet,因为它是空的。我想知道为什么它是空的,即使我在owner-adopt函数中将a的owner设置为this,并在RescueCenter-adopt函数中调用它 所有者类别 void adopt(Animal a) { pet = a; a.owner = this; }
this
,并在RescueCenter-adopt函数中调用它
所有者类别
void adopt(Animal a) {
pet = a;
a.owner = this;
}
救援中心等级:
public boolean adopt(String customerName, String animalName) {
Owner o = new Owner(customerName, age);
Animal d = new Dog(animalType, animalName, animalBreed, animalAge);
if (!o.isQualified() || !d.isQualified()) {
o.qualify();
d.qualify();
}
if (o.isQualified() && d.isQualified()) {
o.adopt(d);
d = o.pet;
adoptableAnimalList.remove(d);
return true;
}
return false;
}
public String getPet(String currCustomerName) {
Owner o = new Owner(currCustomerName, age);
return o.pet.name;
}
您创建了一个
新所有者
,但从未对其调用采用
。(就此而言,您还可以在adopt
中创建并丢弃对象)您可能正在寻找的是类似于Map
的东西,您可以在其中存储和检索Owner
对象。您是否检查了o.adopt(d)代码>实际发生了什么?该漏洞是否在isQualified
中,导致无法识别合格的宠物或主人?或者在中,未能对其进行限定?另外,救援中心#采用
实际上在哪里接到电话?