Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the cause of this nullPointerException?
    text
    copied!<p>When I run my test harness I am getting a NullPointerException on the line:</p> <pre><code>if(flower.extractPollen(pol)){ </code></pre> <p>which I am struggling to explain. In my test harness I am adding flowers to the instance of my garden and the flowers have pollen as far as I can tell from println's. In my test harness I am creating an instance of a garden, a queen bee (in an arraylist) in a hive and adding a rose and a daffodil (in an arraylist) to the garden. I then run another day on the hive and the garden which run another day on the flowers in the garden (adding pollen to them which I can tell is happening from a println) and creating more bees. When a worker bee is created it is supposed to get a flower from the instance of the garden and extract the pollen from it but I get a NullPointerException instead. Relevant code:</p> <pre><code>public class Worker{ public Bee anotherDay(){ for(int u = 0; u&lt; 2; u++){ Flower flower = Garden.getInstance().findFlower(); for(int pol = 5; pol&gt;0; pol--){ if(flower.extractPollen(pol)){ if(pol&gt;1){ hive.addRoyalJelly(1); pol = pol - 2; hive.addHoney(pol); } break; } }//code omitted which returns the bee in this method and some other methods from the class } } public class Garden{ private ArrayList&lt;Flower&gt; flowerbed = new ArrayList&lt;Flower&gt;(); Hive hive = null; private static Garden instance; private Garden(){} public static Garden getInstance(){ if(instance == null){ instance = new Garden(); } return instance; } public void anotherDay(){ int size = flowerbed.size(); for(int i = 0; i &lt; size; i++) { Flower flower = flowerbed.get(i); System.out.println(flower); flower.grow(); } } public Flower getFlower(int fi){ if(fi &lt; flowerbed.size()){ return flowerbed.get(fi); }else{ return null; } } public Integer getRandomInteger(Integer max) { Random rand = new Random(); int number; number = rand.nextInt(max) + 1; return new Integer(number); } public Flower findFlower(){ return getFlower(getRandomInteger(flowerbed.size())); } } public abstract class Flower{ public boolean extractPollen(int po){ if(po &lt;= pollen){ pollen= pollen - po; return true; }return false; } //subclasses are not abstract and in their grow methods pollen is added. Some other methods omitted } </code></pre> <p>By using System.out.println() around my code I can see that the code which I use is adding a rose and adoffodil to the instance of the garden and that by the time the Worker bee is created (on day 14) they have 28 and 42 pollen respectively. As the Worker is only trying to extract 5 I cannot see why I am getting this exception!! The stack trace:</p> <pre><code>Exception in thread "main" java.lang.NullPointerException at Worker.anotherDay(Worker.java:21) at Hive.anotherDay(Hive.java:105) at TestBasicHive.testpart5(TestBasicHive.java:405) at TestBasicHive.main(TestBasicHive.java:14) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload