PacMan - Ghost class

NullPointerException

Page 1 of 1

13 Replies - 14779 Views - Last Post: 24 March 2010 - 05:24 AM Rate Topic: -----

#1 Simple_Condolences   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 180
  • Joined: 10-January 10

PacMan - Ghost class

Posted 22 March 2010 - 03:03 PM

Quote

Alright, so, everything compiles just fine and even puts everything in the grid when I run it. However, before the first step in the program executes, I receive a nullPointerException. Any help squashing this bug would be greatly appreciated.

EDIT: Yes, this is all from the Gridworld API. I -hate- Gridworld.


Quote

A Ghost extends Critter.
When it acts, there is a 20% chance it will move towards a PacMan Critter and a 80% chance it will move randomly in ANY direction (which, by the definition of randomness, may include moving towards a PacMan Critter).
Ghosts eat PacMan critters but nothing else.
When a Ghost moves from a location A to a location B, location A should have a pill if it had one before the ghost, or not have a pill if it didn’t have one before the ghost. (Think about using class level Booleans to accomplish this.)
(If your Ghost Critter doesn’t work correctly, your project score will not be higher than an 80.)


import info.gridworld.actor.Actor;
import info.gridworld.actor.Critter;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
import java.awt.Color;
import java.util.ArrayList;

/**
 *
 * @author Zach
 */

public class Ghost extends Critter {
    Grid<Actor> gr = getGrid();
    private boolean lastMove = true;
    private boolean thisMove = true;

    public Ghost(Color col) {
        setColor(col);
    }

    @Override
    public void act() {
        if(getGrid() == null)
            return;
        ArrayList<Actor> actors = getActors();
        processActors(actors);
        ArrayList<Location> moveLocs = getMoveLocations();
        Location loc = selectMoveLocation(moveLocs);
        makeMove(loc);
    }

    @Override
    public void processActors(ArrayList<Actor> actors) {
        eatPacMan();
        
    }

    public void eatPacMan() {
        Location loc = getLocation();
        Location loc2;
        Actor a;

        for(int dir = 0;dir<360;dir+=15) {
            loc2 = loc.getAdjacentLocation(dir);
            if(gr.isValid(loc2)) {
                a = gr.get(loc2);
                if(a instanceof PacMan)
                    a.removeSelfFromGrid();
            }
        }
    }

    public ArrayList<Location> getMoveLocation() {
        ArrayList<Location> locs = gr.getValidAdjacentLocations(getLocation());
        for(int i = locs.size()-1; i>0 ;i++) {
            if(!(gr.get(locs.get(i)) instanceof PacMan) &&
               !(gr.get(locs.get(i)) instanceof Pill))
                locs.remove(i);
        }
        return locs;
    }

    @Override
    public Location selectMoveLocation(ArrayList<Location> locs) {
        int n = locs.size();
        Location loc = getLocation();
        double r = Math.random();
        if(r  <= 0.2)
           return loc.getAdjacentLocation(loc.getDirectionToward(findPacMan()));
        else
            return locs.get((int)(Math.random() * n));
    }

    @Override
    public void makeMove(Location loc) {
        Location loc2 = getLocation();
        if(loc == null)
            removeSelfFromGrid();
        else {
            if(gr.get(loc) instanceof Pill)
                thisMove = true;
            else
                thisMove = false;

            moveTo(loc);

            if(lastMove == true)
                gr.put(loc2, new Pill());
        }
        lastMove = thisMove;
    }

    public Location findPacMan() {
        Location loc = new Location(0, 0);
        ArrayList<Actor> actors = getActors();
        for(Actor a: actors) {
            if(a instanceof PacMan)
                loc = a.getLocation();
        }
        return loc;
    }
}



Error thrown:

Quote

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Ghost.eatPacMan(Ghost.java:47)
at Ghost.processActors(Ghost.java:36)
at Ghost.act(Ghost.java:28)
at info.gridworld.actor.ActorWorld.step(ActorWorld.java:68)
at info.gridworld.gui.GUIController.step(GUIController.java:134)
at info.gridworld.gui.GUIController$4.actionPerformed(GUIController.java:247)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.window.dispatchEventImpl(window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


Thrown at:
public void eatPacMan() {
        Location loc = getLocation();
        Location loc2;
        Actor a;

        for(int dir = 0;dir<360;dir+=15) {
            loc2 = loc.getAdjacentLocation(dir);
            if(gr.isValid(loc2)) { //line 47
                a = gr.get(loc2);
                if(a instanceof PacMan)
                    a.removeSelfFromGrid();
            }
        }
    }


Requested code:
public boolean isValid(Location loc) {
    return 0 <= loc.getCol() && loc.getRow() < getNumRows()
            && 0 <= loc.getCol() && loc.getCol() < getNumCols();
}

//I am not given the source code for the below method

public Location getAdjacentLocation(int direction)
        returns the adjacent location in the direction that is closest to direction




Quote

-E get(Location loc) returns an object at the Location loc
-E getAdjacentLocation(Direction dir) returns a location in direction dir from object E
-E getLocation() returns the location of object E
- getActors() returns an ArrayList<Actor> of all actors on the grid
- getGrid() returns the grid of type Actor which all of the objects lie in


I can provide the source code for all of these methods and any others in this program if needed. Thanks for taking a look.

- Zach

This post has been edited by Simple_Condolences: 22 March 2010 - 07:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: PacMan - Ghost class

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: PacMan - Ghost class

Posted 22 March 2010 - 05:26 PM

I notice fairly often that you do not validate the adjacent Location. It is most likely one of these that is causing your NullPointerExceptions. If you could post the error message and the code at that line, we might be able to help you more.
Actor a = gr.get(loc.getAdjacentLocation(getDirection())); 


Was This Post Helpful? 0
  • +
  • -

#3 Simple_Condolences   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 180
  • Joined: 10-January 10

Re: PacMan - Ghost class

Posted 22 March 2010 - 07:05 PM

Main post updated. Current code and error messages are posted.

Thanks for everything.

- Zach
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: PacMan - Ghost class

Posted 22 March 2010 - 07:54 PM

Somehow, it seems that loc2 is null when you invoke the getAdjacentLocation() method. You may want to try testing it for a null reference before invoking the Grid isValid() method.

Quote

public boolean isValid(Location loc) { 
    return 0 <= loc.getCol() && loc.getRow() < getNumRows() 
            && 0 <= loc.getCol() && loc.getCol() < getNumCols(); 
} 
 
//I am not given the source code for the below method 
 
public Location getAdjacentLocation(int direction) 
        returns the adjacent location in the direction that is closest to direction


What are you talking about not given the source code? Both methods are part of the GridWorld framework. The isValid() method is part of the Grid<E> interface, and getAdjacentLocation() is part of the Location class.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Simple_Condolences*


Reputation:

Re: PacMan - Ghost class

Posted 22 March 2010 - 08:14 PM

That is correct, however the source code is not provided within the API. I did some digging though and here it is:
public Location getAdjacentLocation(int direction)
    {
        // reduce mod 360 and round to closest multiple of 45
        int adjustedDirection = (direction + HALF_RIGHT / 2) % FULL_CIRCLE;
        if (adjustedDirection < 0)
            adjustedDirection += FULL_CIRCLE;

        adjustedDirection = (adjustedDirection / HALF_RIGHT) * HALF_RIGHT;
        int dc = 0;
        int dr = 0;
        if (adjustedDirection == EAST)
            dc = 1;
        else if (adjustedDirection == SOUTHEAST)
        {
            dc = 1;
            dr = 1;
        }
        else if (adjustedDirection == SOUTH)
            dr = 1;
        else if (adjustedDirection == SOUTHWEST)
        {
            dc = -1;
            dr = 1;
        }
        else if (adjustedDirection == WEST)
            dc = -1;
        else if (adjustedDirection == NORTHWEST)
        {
            dc = -1;
            dr = -1;
        }
        else if (adjustedDirection == NORTH)
            dr = -1;
        else if (adjustedDirection == NORTHEAST)
        {
            dc = 1;
            dr = -1;
        }
        return new Location(getRow() + dr, getCol() + dc);
    }



- Zach
Was This Post Helpful? 0

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: PacMan - Ghost class

Posted 22 March 2010 - 08:19 PM

Don't worry, I'm in AP CS too this year, so I have my own copy of GridWorld. It just sounded from what you were saying that you didn't have access to the source code for the GridWorld classes; which if you have the framework, isn't shouldn't be the case.
Was This Post Helpful? 0
  • +
  • -

#7 Guest_Simple_Condolences*


Reputation:

Re: PacMan - Ghost class

Posted 23 March 2010 - 05:48 AM

Nope, I have it along with my API, I just had to go digging for the source in the framework.

Don't you just -love- Gridworld? Its been pretty much the only thing I haven't been able to grasp this year. I dunno. Everything I do throws nullPointers. But looking at it, I can't see a flaw in my logic for certain lines. Especially not for an isValid null pointer... that should only return false if the location to be checked is null... So I don't even know...

- Zach
Was This Post Helpful? 0

#8 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: PacMan - Ghost class

Posted 23 March 2010 - 08:10 AM

I.HATE.GRIDWORLD.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: PacMan - Ghost class

Posted 23 March 2010 - 08:52 AM

Me too. At least the FRQ isn't graded by a compiler, and the Multiple Choice questions are a little more straight forward. We just finished with GridWorld today in AP CS (which marks the end of any new material for the AP exam), and are starting to review. Hopefully next marking period we'll go onto some AB level topics.
Was This Post Helpful? 0
  • +
  • -

#10 Guest_Simple_Condolences*


Reputation:

Re: PacMan - Ghost class

Posted 23 March 2010 - 09:15 PM

View PostDogstopper, on 23 March 2010 - 07:10 AM, said:

I.HATE.GRIDWORLD.

Can I quote you in my signature? lolol. I despise gridworld. Logic that isn't flawed has flaws... it makes NO sense... God I can't wait to be done... we have our test on it tomorrow and then we move on to recursion I think... kinda sucks we don't cover exception handling... Oh well! We've got tutorials for a reason!

- Zach
Was This Post Helpful? 0

#11 Guest_Simple_Condolences*


Reputation:

Re: PacMan - Ghost class

Posted 23 March 2010 - 09:25 PM

View Postmacosxnerd101, on 23 March 2010 - 07:52 AM, said:

Me too. At least the FRQ isn't graded by a compiler, and the Multiple Choice questions are a little more straight forward. We just finished with GridWorld today in AP CS (which marks the end of any new material for the AP exam), and are starting to review. Hopefully next marking period we'll go onto some AB level topics.

Where do you go to school? And I wonder if our AP exam has recursion on it... haha I dunno. I'll find out tomorrow morning.

- Zach
Was This Post Helpful? 0

#12 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: PacMan - Ghost class

Posted 23 March 2010 - 09:53 PM

We are issued laptops for the school year by the county, so that's why we're so far ahead. And yes- recursion is on the AP Exam, and it's fairly big on the multiple choice.
Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: PacMan - Ghost class

Posted 24 March 2010 - 03:19 AM

View PostSimple_Condolences, on 23 March 2010 - 11:15 PM, said:

View PostDogstopper, on 23 March 2010 - 07:10 AM, said:

I.HATE.GRIDWORLD.

Can I quote you in my signature? lolol. I despise gridworld. Logic that isn't flawed has flaws... it makes NO sense... God I can't wait to be done... we have our test on it tomorrow and then we move on to recursion I think... kinda sucks we don't cover exception handling... Oh well! We've got tutorials for a reason!

- Zach


Yes! Feel free!

View PostSimple_Condolences, on 23 March 2010 - 11:25 PM, said:

View Postmacosxnerd101, on 23 March 2010 - 07:52 AM, said:

Me too. At least the FRQ isn't graded by a compiler, and the Multiple Choice questions are a little more straight forward. We just finished with GridWorld today in AP CS (which marks the end of any new material for the AP exam), and are starting to review. Hopefully next marking period we'll go onto some AB level topics.

Where do you go to school? And I wonder if our AP exam has recursion on it... haha I dunno. I'll find out tomorrow morning.

- Zach


Yes, national exam so it will have recursion in it. Perhaps in the free-write section, you my have to write one.
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: PacMan - Ghost class

Posted 24 March 2010 - 05:24 AM

Recursion is usually a multiple choice topic, not free-response. It's tested more in a manner of "given this method and inputs, what is the output" type question.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1