Java 空指针错误帮助?
嗨,我在进程中标记了一个空指针。tester类位于底部。它发生在试图打印出动物园的时候。 似乎“动物”没有名字,但我创造了动物 它可能是访问了我想要的错误的动物,但是我如何访问我制作的列表中的动物(请不要使用for参数中的“:”)! 我知道这是错误的,但类似动物名单的东西。打印详细信息Java 空指针错误帮助?,java,list,class,methods,nullpointerexception,Java,List,Class,Methods,Nullpointerexception,嗨,我在进程中标记了一个空指针。tester类位于底部。它发生在试图打印出动物园的时候。 似乎“动物”没有名字,但我创造了动物 它可能是访问了我想要的错误的动物,但是我如何访问我制作的列表中的动物(请不要使用for参数中的“:”)! 我知道这是错误的,但类似动物名单的东西。打印详细信息 public class Zoo { private Animal animal; public int number_animals;//# animals allowed in zoo
public class Zoo {
private Animal animal;
public int number_animals;//# animals allowed in zoo
private List<Animal> animal_list;
private String zoo_name;
public Zoo(String name){
this.zoo_name = name;
animal_list = new ArrayList<Animal>();
}
public void addAnimal(Animal obj) {
animal_list.add(obj);
}
public String toString(){
String s = "In zoo " + zoo_name + " there are the following animals:\n";
for (int i = 0; i < animal_list.size(); i++){
s += animal.getName() + " who weighs " + animal.getWeight() + " kg.";//null pointer on this line why??? i have made an animal. How do I access this animals in the list (please no using the ":" in the for parameter)!
}
return s;
}
public class Animal {
public String name;
public int weight;
private String food_type;
private int space_requirement;
private Zoo zoo;
private Animal animal;
public Animal(String name, int weight){
this.name = name;
this.weight = weight;
}
public String getName(){
return this.name;
}
public int getWeight(){
return this.weight;
}
public class Test {
public static void main(String[] args) {
Zoo diego = new Zoo("San Diego");
Animal giffy = new Animal("Giffy", 950);
Animal gunther = new Animal("Gunther", 950);
diego.addAnimal(giffy);
diego.addAnimal(gunther);
System.out.println(diego);
}
公共类动物园{
私人动物;
公共整数动物;允许动物进入动物园
私人名单动物名单;
私有字符串名称;
公共动物园(字符串名称){
this.zoo_name=名称;
animal_list=新数组列表();
}
公共动物(动物对象){
动物列表。添加(obj);
}
公共字符串toString(){
字符串s=“In zoo”+zoo\u name+“有以下动物:\n”;
对于(int i=0;i
还请注意,您确实应该在此处使用StringBuilder
,而不是使用+=
和+
运算符创建新的String
对象:
public String toString() {
StringBuilder s = new StringBuilder("In zoo ");
s.append(zoo_name).append(" there are the following animals:\n");
for (int i = 0; i < animal_list.size(); i++){
s.append(animal_list.get(i).getName());
s.append(" who weighs ");
s.append(animal_list.get(i).getWeight());
s.append(" kg.\n");
}
return s.toString();
}
这相当于:
Animal a = animal_list.get(i);
String name = a.getName();
还请注意,您确实应该在此处使用StringBuilder
,而不是使用+=
和+
运算符创建新的String
对象:
public String toString() {
StringBuilder s = new StringBuilder("In zoo ");
s.append(zoo_name).append(" there are the following animals:\n");
for (int i = 0; i < animal_list.size(); i++){
s.append(animal_list.get(i).getName());
s.append(" who weighs ");
s.append(animal_list.get(i).getWeight());
s.append(" kg.\n");
}
return s.toString();
}
这相当于:
Animal a = animal_list.get(i);
String name = a.getName();
为什么动物是一个类级属性?将循环更改为
for(animal-animal:animal_-list)
我想这应该可以解决它为什么动物是一个类级属性?将循环更改为for(animal-animal:animal_-list)
我想应该可以解决toString()中的问题
方法,用于在从未创建的动物对象上调用的Zoo类getName()
和getWeight()
在for循环中,您需要以下内容:
public String toString(){
String s = "In zoo " + zoo_name + " there are the following animals:\n";
for (int i = 0; i < animal_list.size(); i++)
{
animal = animal_list.get(i); //changes:
//stores the animal object at index i
//in the animal variable
s += animal.getName() + " who weighs " + animal.getWeight()
+ " kg.";
}
return s;
公共字符串toString(){
字符串s=“In zoo”+zoo\u name+“有以下动物:\n”;
对于(int i=0;i
}
希望这有帮助。在动物园类的toString()
方法中,您正在调用getName()
和getWeight()
一个从未创建过的动物对象
在for循环中,您需要以下内容:
public String toString(){
String s = "In zoo " + zoo_name + " there are the following animals:\n";
for (int i = 0; i < animal_list.size(); i++)
{
animal = animal_list.get(i); //changes:
//stores the animal object at index i
//in the animal variable
s += animal.getName() + " who weighs " + animal.getWeight()
+ " kg.";
}
return s;
公共字符串toString(){
字符串s=“In zoo”+zoo\u name+“有以下动物:\n”;
对于(int i=0;i
}
希望这有帮助。您的循环在此:
for (int i = 0; i < animal_list.size(); i++){
s += animal.getName() + " who weighs " + animal.getWeight() + " kg.";
}
这可以使用以下结构更习惯地编写
for (Animal a : animal_list){
s += a.getName() + " who weighs " + a.getWeight() + " kg.";
}
您的循环在此:
for (int i = 0; i < animal_list.size(); i++){
s += animal.getName() + " who weighs " + animal.getWeight() + " kg.";
}
这可以使用以下结构更习惯地编写
for (Animal a : animal_list){
s += a.getName() + " who weighs " + a.getWeight() + " kg.";
}
问题是您没有访问列表中的动物。您正在尝试打印您在zoo类中声明但尚未实例化的动物的详细信息。这里有您的NullPointerException
public String toString(){
String s = "In zoo " + zoo_name + " there are the following animals:\n";
for (Animal animal:animal_list){
s += animal.getName() + " who weighs " + animal.getWeight() + " kg.";//null pointer on this line why??? i have made an animal. How do I access this animals in the list (please no using the ":" in the for parameter)!
}
return s;
}
问题是您没有访问列表中的动物。您正在尝试打印您在zoo类中声明但尚未实例化的动物的详细信息。这里有您的NullPointerException
public String toString(){
String s = "In zoo " + zoo_name + " there are the following animals:\n";
for (Animal animal:animal_list){
s += animal.getName() + " who weighs " + animal.getWeight() + " kg.";//null pointer on this line why??? i have made an animal. How do I access this animals in the list (please no using the ":" in the for parameter)!
}
return s;
}
他明确地说他不想那样做。为什么我不能告诉你,他留下来阅读他在代码中的评论,以找到这个和问题,但问题就在那里。@brian,谢谢你指出这一点,但我想你应该质疑他的推理,而不是否决解决问题的方案!对不起,但我坚持让他删除类级别属性,并将循环更改为“for each construct”。我很高兴听到为什么不应该这样做。然后在评论中询问他为什么有此要求是适当的做法,而不是做出假设并提供OP明确表示不是他想要的答案。或者,提供他要求的解决方案,然后还说明了为什么为每个构造使用一个,做同样的事情。如果我不得不猜测这是因为这是家庭作业,但我必须问OP。他明确表示他不想这样做。为什么我不能告诉你,他让他阅读代码中的注释来找到这一点和问题,但问题就在那里@brian,谢谢你指出这一点,但我想你应该质疑他的推理,而不是否决解决他的问题的方法!抱歉,但我会坚持要求他删除类级属性,并将循环更改为a for each构造。我很高兴他这么做