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!
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
}
Stacktrace:
Exception in thread "main" java.lang.NullPointerException
at Animal.Goose.<init>(Bee.java:26)
at Animal.DrawingPanel.<init>(DrawingPanel.java:30)
at Animal.ControlPanel.<init>(ControlPanel.java:27)
at Animal.App.<init>(App.java:28)
at Animal.App.main(App.java:39)
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.
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);
Its hard to say without a stack trace and incomplete code example, but there’s multiple potential problems with your code:
I would guess
_gooseBehaviouris null, but you can help us answer your question if you include the full stack trace (The error that was printed out that let you know you had a null pointer exception).EDIT
Based off your NPE I’ll bet dollar’s to donuts that your holder is getting passed into the constructor as a null. Inspect it’s value in your debugger or print out its value to verify.