Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6761745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:13:43+00:00 2026-05-26T14:13:43+00:00

I can’t figure out how to fix my null pointer. My holder (Holder Pattern

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:13:43+00:00Added an answer on May 26, 2026 at 2:13 pm

    Its hard to say without a stack trace and incomplete code example, but there’s multiple potential problems with your code:

    public Goose (JPanel container, GooseBehavior behavior, BehaviorHolder holder) {
        super(container); 
    
        // removed some code for clarity
    
        _gooseBehavior = behavior;                     // <<- You store behaviour here
        _gooseBehavior = _holder.getBehavior();        // <<- then immediately override it
    }
    
    public void react() {
        _gooseBehavior.stop();
        _holder.getBehavior();                  // <-- this isn't getting assigned to anything
        _gooseBehavior = _holder.getBehavior(); // A duplicate assignment, previously done in constructor. Are you changing holder's behaviour outside of the display class?
    
        _gooseBehavior.setTarget(this); //sets the target on goose
        _gooseBehavior.start(); //starts the behavior    
    }
    

    I would guess _gooseBehaviour is 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can I call a java class method from inside a flash movie?
Can anyone help me as what is going wrong here? Am not able to
Can someone tell me why the following does not match: >>> re.search(r'(\d{2, 10})', '153')
can anyone tell me why this doesn't work? db = openOrCreateDatabase(database.db, SQLiteDatabase.CREATE_IF_NECESSARY, null); db.setLocale(Locale.getDefault());
Can't work out a way to make an array of buttons in android. This
Can anyone help me trying to find out why this doesn't work. The brushes
can someone show me the regex for this preg_match. I want to make sure
Can you set the internal [[Class]] property of an ECMAScript object?
Can somebody point me to a resource that explains how to go about having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.