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 6781485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:37:46+00:00 2026-05-26T16:37:46+00:00

I am new to dealing with Java and the course I’m taking is showing

  • 0

I am new to dealing with Java and the course I’m taking is showing me some code, but when I am trying to run it. It returns a null pointer exception due to the fact that the parent was never set.

So in an abstract class how do I pass in the Parent? This is based on AI searches! Thanks!!

Heres the code:

This is based on the farmer wolf goat cabbage problem.

AbstractState:

package hw1;

public abstract class AbstractState implements State
{
    public State parent = null;
    private double distance = 0;

    public AbstractState(){}
    public AbstractState(State Parent)
    {
        this.parent = parent;
        this.distance = parent.getDistance() +1;
    }

    public State getParent()
    {
        return parent;
    }

    public double getDistance()
    {
        return distance;
    }

}

State

package hw1;
import java.util.Set;


public interface State 
{
    public Iterable<State> getPossibleMoves();
    public boolean isSolution();
    public double getHeuristic();
    public double getDistance();
    public State getParent();
}

FarmerWolfGoatCabbage:

package hw1;
import java.util.HashSet;
import java.util.Set;


public class FarmerWolfGoatState extends AbstractState 
{
    enum Side
    {
        EAST 
        {
            public Side getOpposite()
            {
                return WEST;
            }
        },
        WEST
        {
            public Side getOpposite()
            {
                return EAST;
            }
        };

        abstract public Side getOpposite();
    }

    private Side farmer = Side.EAST;
    private Side wolf = Side.EAST;
    private Side goat = Side.EAST;
    private Side cabbage = Side.EAST;

    public FarmerWolfGoatState()
    {}

    public FarmerWolfGoatState(FarmerWolfGoatState parent, Side farmer, Side wolf, Side goat, Side Cabbage)
    {
        super(parent);
        this.farmer = farmer;
        this.wolf = wolf;
        this.goat = goat;
        this.cabbage = cabbage;
    }

    @Override
    public Iterable<State> getPossibleMoves() {
        Set<State> moves = new HashSet<State>();

        if(farmer == wolf)
            new FarmerWolfGoatState(this,
                    farmer.getOpposite()
                    ,wolf.getOpposite()
                    ,goat,cabbage).addIfSafe(moves);

        if(farmer == goat)
            new FarmerWolfGoatState(this,
                    farmer.getOpposite()
                    ,wolf
                    ,goat.getOpposite(),cabbage).addIfSafe(moves);

        if(farmer == cabbage)
            new FarmerWolfGoatState(this,
                    farmer.getOpposite()
                    ,wolf
                    ,goat,cabbage.getOpposite()).addIfSafe(moves);

        new FarmerWolfGoatState(this, farmer.getOpposite(), wolf, goat, cabbage).addIfSafe(moves);

        return moves;

    }

    @Override
    public boolean isSolution() {
        //returns true if all of the them are on the west side and not the east.
        return farmer == Side.WEST && wolf == Side.WEST && goat==Side.WEST && cabbage == Side.WEST;

    }

    @Override
    public double getHeuristic() {
        // TODO Auto-generated method stub
        return 0;
    }

    public final void addIfSafe(Set<State> moves)
    {
        boolean unsafe = (farmer!= wolf && farmer != goat) || (farmer != goat && farmer != cabbage);

        if(!unsafe)
            moves.add(this);
    }

    public boolean equals(Object o)
    {
        if(o == null || !(o instanceof FarmerWolfGoatState))
            return false;
        FarmerWolfGoatState fwgs = (FarmerWolfGoatState)o;

        return farmer == fwgs.farmer &&
                wolf == fwgs.wolf &&
                cabbage == fwgs.cabbage &&
                goat == fwgs.goat;
    }

    public int hashCode()
    {
        return(farmer == Side.EAST ? 1 : 0) +
                (wolf == Side.EAST ? 2 : 0) +
                (cabbage == Side.EAST ? 4: 0)+
                (goat == Side.EAST ? 8 : 0);
    }

}

Main trying to solve..

package hw1;

import hw1.FarmerWolfGoatState.Side;



public class Homework1 {

    /**
     * @param args
     */
    public static void main(String[] args) 
    {
        FarmerWolfGoatState parentState = new FarmerWolfGoatState();
        FarmerWolfGoatState nextState = new FarmerWolfGoatState(parentState,Side.EAST,Side.EAST,Side.EAST,Side.WEST);

        while(!nextState.isSolution())
        {
            nextState.getPossibleMoves();
        }

    }


}

Stack Trace:

Exception in thread "main" java.lang.NullPointerException
    at hw1.AbstractState.<init>(AbstractState.java:12)
    at hw1.FarmerWolfGoatState.<init>(FarmerWolfGoatState.java:38)
    at hw1.Homework1.main(Homework1.java:15)

I was also given a solver, should I use that?

here it is.
package hw1;
import java.util.Stack;


public class DepthFirstSolver extends AbstractSolver
{
    private Stack<State> stack = new Stack<State>();

    @Override
    protected void addState(State s)
    {
        if(!stack.contains(s))
        {
            stack.push(s);
        }
    }

    @Override
    protected boolean hasElements()
    {
        return !stack.empty();
    }

    @Override
    protected State nextState()
    {
        return stack.pop();
    }
    @Override
    protected void clearOpen()
    {
        stack.clear();
    }

}


package hw1;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;




public abstract class AbstractSolver implements Solver 
{
    private Set<State> closed= new HashSet<State>();

    public List<State> solve(State initialState)
    {
        closed.clear();
        clearOpen();
        while(hasElements())
        {
            State s = nextState();
            if(s.isSolution())
                return findPath(s);
            closed.add(s);
            Iterable<State> moves = s.getPossibleMoves();
            for(State move : moves)
                if(!closed.contains(move))
                        addState(move);
        }
        return null;

    }
    public int getVisitedStateCount()
    {
        return closed.size();
    }

    private List<State> findPath(State solution)
    {
        LinkedList<State> path = new LinkedList<State>();
        while(solution!=null)
        {
            path.addFirst(solution);
            solution = solution.getParent();

        }
        return path;
    }

    protected abstract boolean hasElements();
    protected abstract State nextState();
    protected abstract void addState(State s);
    protected abstract void clearOpen();

}
  • 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-26T16:37:47+00:00Added an answer on May 26, 2026 at 4:37 pm

    Looks like a uppercase/lowercase problem, you need to change the constructor of AbstractState to

    public AbstractState(State parent)
    

    instead of

    public AbstractState(State Parent)
    

    On the line this.distance = parent.getDistance() +1;, your constructor is using the uninitialized instance variable of name parent instead of the input parameter of name Parent.

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

Sidebar

Related Questions

Usually, when dealing with Java IO code, here is what I wrote FileOutputStream out
I was dealing with hibernate, trying to figure out the run-time class behind proxied
I'm dealing with a null pointer exception for which I have a stack trace.
I am new to Java and Hibernate and I am trying to map the
I'm dealing here with a new problem. I started with the following code: -(IBAction)
I'm new to Lua and dealing with Lua as a scripting language in an
New to both Ruby and Rails but I'm book educated by now (which apparently
New to WCF, but familiar with COM+ - can I wrap a WCF service
New to Linux programming in general. I am trying to communicate with a kernel
New day, new problem :-) Code: Client Side: void abw_Closed(object sender, EventArgs e) {

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.