Note that there are some explanatory texts on larger screens.

plurals
  1. PONull pointer exception error that I am having trouble figuring out?
    primarykey
    data
    text
    <p>I can't figure out how to fix my null pointer. My holder (Holder Pattern class) is undefined - I'm not sure how to fix it. I am still pretty new to programming. Thanks for any ideas on how to go about fixing this! </p> <pre><code>public Goose (JPanel container, GooseBehavior behavior, BehaviorHolder holder) { super(container); _holder = holder; double rnd = Math.random(); //local variable to create random angle //System.out.println(rnd); this.setRotation(rnd);//sets the rotation angle to a random angle this.setSize(25, 20); //sets size this.setFillColor(java.awt.Color.RED); //sets color this.setWrapping(true); //sets wrapping to true _gooseBehavior = behavior; //stores _gooseehavior _gooseBehavior = _holder.getBehavior(); } public void react() { _gooseBehavior.stop(); //tells the current _gooseBehavior to stop _holder.getBehavior(); //gets the stored behavior from the holder _gooseBehavior = _holder.getBehavior(); //sets the new value to one stored in holder _gooseBehavior.setTarget(this); //sets the target on goose _gooseBehavior.start(); //starts the behavior } </code></pre> <p>Stacktrace:</p> <pre><code>Exception in thread "main" java.lang.NullPointerException at Animal.Goose.&lt;init&gt;(Bee.java:26) at Animal.DrawingPanel.&lt;init&gt;(DrawingPanel.java:30) at Animal.ControlPanel.&lt;init&gt;(ControlPanel.java:27) at Animal.App.&lt;init&gt;(App.java:28) at Animal.App.main(App.java:39) </code></pre> <p>Here's the code where I instantiate BehaviorHolder in my Control Panel (top level object), and then I just have it stored in my drawingPanel, Goose class, and BehaviorButtons so that its associated with the three. </p> <pre><code>public ControlPanel() { super(); this.setLayout(new BorderLayout());//sets a new BorderLayout _drawingPanel = new DrawingPanel(_holder); _moveRandomly = new MoveBehavior(); _doNothing = new StopBehavior(); _kingGoose = new FollowBehavior(_kingGoose); _holder = new BehaviorHolder(_moveBehavior); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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