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

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

问题描述

在我的程序中,我有一个名为 Hive 的类,其中包含蜂蜜、花粉、蜂王浆和几种蜜蜂的数组列表.一些蜜蜂类型有一种吃法,它们之间几乎相同,区别在于它们吃的东西或吃的数量.每只蜜蜂都有一个 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);
    }

Queen类:(不要在这个类的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)

我将元素添加到数组列表中,代码运行良好,直到幼虫/工人或无人机出现并对它们尝试吃方法.如果我注释掉运行 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)

根据给出的答案,我尝试将工人类中的构造函数更改为:

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

我需要空"构造函数,因为工蜂是从蛹中的 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 方法将其添加到数组列表中.

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天全站免登陆