Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

java - What is the reason for my 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:

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...