完整代码在这里:https://pastebin.com/ntSZ3wZZ 好吧,所以一些链接列表程序中的构造函数一定会出现可怕的错误,我尝试创建。Java对象链表 - 空指针异常
这里是我的程序是应该做的:
// running
add 3.0 3.0 Three
add 2.0 2.0 Two
add 1.0 1.0 One
print
One{0} +1.0, +1.0
Two{0} +2.0, +2.0
Three{0} +3.0, +3.0
这里是发生了什么:
add 3.0 3.0 Three
Exception in thread "main" java.lang.NullPointerException
at Pet.setLat(Pet.java:37)
at Pet.newPet(Pet.java:24)
at Pet.<init>(Pet.java:18)
at PetList.insertFront(PetList.java:23)
at Exe.main(Exe.java:14)
我觉得像我使用的是空引用(如果这就是你怎么称呼它) 。但我无法弄清楚在哪里或如何!我知道这是一个模糊的问题,但我不知道要问什么。如果我可以对我的问题进行一些编辑以使其更容易,请让我知道。感谢您的任何帮助!
下面是我的一些代码:
public Pet() {
name = "";
treats = 0;
coor = new Coordinate();
}
public Pet(Pet copy) {
if(copy == null) {
name = "";
treats = 0;
coor = new Coordinate();
return;
}
newPet(copy);
}
public void newPet(Pet copyTwo) {
setName(copyTwo.name);
setTreats(copyTwo.treats);
setLat(copyTwo.getLat()); // error here line 24
setLong(copyTwo.getLong());
}
public void setLat(float newLat) {
coor.setLatitude(newLat);
}
在未来,你应该在你的问题,从你的程序中相关的源代码。不要在其他网站上链接到它。 – Kenster
不要告诉我们**你的代码应该做什么**,但代码**本身** – QBrute
你的错误在setLat方法中 - 你需要发布setLat的代码。 –