What is the reason for my NullPointerException?

萝らか妹 提交于 2020-03-26 05:22:28

问题


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:

Hive class:

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);
    }

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; 
    }
}

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;        
    }
}

The Exception:

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)

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)

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(){}

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;
}

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


回答1:


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



来源:https://stackoverflow.com/questions/20457931/what-is-the-reason-for-my-nullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!