我的NullPointerException是什么原因? [英] What is the reason for my NullPointerException?

查看:112
本文介绍了我的NullPointerException是什么原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我有一个名为Hive的类,其中包含Honey,Pollen,royalJelly和几种蜜蜂的数组列表.某些蜜蜂的食用方法彼此之间几乎相同,不同之处在于它们的食用量或食用量.每只蜜蜂都有一个anotherDay方法,该方法调用蜜蜂上的eat方法,在蜂巢的anotherDay方法中,它循环遍历数组列表,从而调用每只蜜蜂anotherDay.我的皇后蜂吃得很好,但是当我在工人,无人机或幼虫上吃东西时,我得到了NullPointerException,我不知道为什么!我的代码:

In my program I have a class called Hive which contains Honey, Pollen, royalJelly and an arraylist of several types of bees. Some of the bee types have an eat method which is pretty much identical between them with the difference being in what they eat or the quantity they eat. Each bee has an anotherDay method which calls the eat method on the bee and in thehive's anotherDay method it loops through the arraylist calling each bees anotherDay. My Queen bees eat runs perfectly however, when I run eat on my worker, drone or larvae I get a NullPointerException and I can't figure out why! My code:

蜂巢类:

public class Hive {

    ArrayList<Bee> cells = new ArrayList<Bee>();
    int honey = 10;
    int royalJelly = 10;
    int pollen = 10;             //some methods omitted 

    public void anotherDay(){
        ArrayList<Bee> dead = new ArrayList<Bee>();
        int size = cells.size();

        for(int i = 0; i < size; i++) {
            Bee bee = cells.get(i);

            if ((bee = bee.anotherDay()) == null) {
                dead.add(cells.get(i));
            } else {
                cells.set(i, bee);
            }
        }
        cells.removeAll(dead);
    }

女王类:(不要从此类中的eat方法获取空指针异常)

Queen Class: (don't get null pointer exception from the eat method in this class)

public class Queen extends Bee{

    Hive hive = null;

    public Queen(){
    }

    public Queen(Hive hive){
        this.hive = hive;
        this.type = 1;
        this.age = 11;
        this.health = 3;
    }

    public Bee anotherDay(){
        eat();
        if (health == 0) {
            return null;
        }
        age++;
        if (age % 3 == 2) {
            hive.addBee(new Egg());
        }
        return this;
    }

    public boolean eat(){
        if(hive.honey >= 2) {
            hive.takeHoney(2);
            if(health < 3){
                health++;
            }
            return true;
        }
        health -= 1;
        return false; 
    }
}

工蜂类:(获取NullPointerException-不知道为什么)

Worker bee class: (get a NullPointerException- not sure why)

public class Worker extends Bee {

    Hive hive = null; 

    public Worker(){
        this.type = 2;
        this.age=11;
        this.health=3;
    }

    public Bee anotherDay(){
        eat();
        age++;
        if (health == 0) {
            return null;
        }
        return this;
    }

    public boolean eat(){
        if(hive.honey >= 1) {
            hive.takeHoney(1);
            if(health < 3){
                health++;
            }
            return true;
        }
        health -= 1;
        return false;        
    }
}

例外:

Exception in thread "main" java.lang.NullPointerException
    at Larvae.eat(Larvae.java:26)
    at Larvae.anotherDay(Larvae.java:13)
    at Hive.anotherDay(Hive.java:86)
    at TestBasicHive.testpart4(TestBasicHive.java:381)
    at TestBasicHive.main(TestBasicHive.java:13)

我将元素添加到arraylist中,代码运行良好,直到幼虫/工人或无人机出现并尝试使用eat方法.如果我注释掉运行eat方法的位,它将运行良好(但显然不执行我想要的操作)

I add elements to the arraylist and the code runs fine until larvae/ workers or drones come up and the eat method is attempted on them. If I comment out the bit which runs the eat method it will run fine (but obviously not do what I want it to do)

从给出的答案中,我尝试将worker类中的构造函数更改为:

From the answers given I have tried changing the constructor in my worker class to:

public Worker(Hive hive){
    this.hive=hive;
    this.type = 2;
    this.age=11;
    this.health=3;
}

public Worker(){}

我需要'empty'构造函数,因为工蜂是通过pupa中的anotherDay方法添加到蜂巢中的,该方法是:

I need the 'empty' constructor as the Worker bees are added to the hive from the anotherDay method in pupa which is:

public Bee anotherDay(){
    age++;
    if(age>10){
        return new Worker();
    }return this;
}

然后通过hive中的anotherDay方法将其添加到arraylist中.

This is then added to the arraylist via the anotherDay method in hive.

推荐答案

hive 初始化为 null ,并且在您的 Worker 类中从未更改.然后在 eat 方法中尝试访问一个字段.

hive is initialized to null and never changed in your Worker class. Then in the eat method you try to access a field.

这篇关于我的NullPointerException是什么原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆