This site requires JavaScript, please enable it in your browser!
Greenfoot back
Will5230
Will5230 wrote ...

2018/12/14

Keep getting null pointer exception

Will5230 Will5230

2018/12/14

#
I am trying to make pacman eat the pellets. I am quite new to greenfoot and keep getting java.lang.NullPointerException at Pacman.eat(Pacman.java:77) at Pacman.act(Pacman.java:37) as an error. Here is my Pacman code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Pacman here. * * @author (your name) * @version (a version number or a date) */ public class Pacman extends Actor { /** * Act - do whatever the Pacman wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Pacman() { GreenfootImage image = getImage(); image.scale(35, 35); setImage(image); } public void act() { controls(); move(-3); eat(); } private void controls() { if ( Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("D") ) { setRotation(-180); } if ( Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("A") ) { setRotation(-0); } if ( Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("W") ) { setRotation(-270); } if ( Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown("S") ) { setRotation(-90); } } private boolean checkForWall(int x, int y) { int origX = getX(); int origY = getY(); // Finds a wall object at the offset to the player. setLocation(x , y); Actor wall = getOneIntersectingObject( Wall.class ); setLocation(origX, origY); return (wall != null); } public void eat() { Actor Pellet = getOneObjectAtOffset(0, 0,Pellet.class); if(Pellet != null) { World world = getWorld(); world.removeObject(Pellet); ((MyWorld)world).counter.add(1); } } } Pellet code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Pellet here. * * @author (your name) * @version (a version number or a date) */ public class Pellet extends Actor { /** * Act - do whatever the Pellet wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { eat(); } public Pellet() { GreenfootImage image = getImage(); image.scale(45, 35); setImage(image); } public void eat() { Actor Pacman = getOneObjectAtOffset(0, 0,Pellet.class); if(Pacman != null) { World world = getWorld(); world.removeObject(this); ((MyWorld)world).counter.add(1); } } } My world code is pretty much empty.
CoolSharkCody CoolSharkCody

2018/12/14

#
I highly recommend adding a subclass to World. Sometimes the Actor classes have to rely on the subclass of World in order to function properly.
danpost danpost

2018/12/14

#
If I counted correctly, it was the counter line that threw the error. Show your World subclass code for review.
Will5230 Will5230

2018/12/17

#
Ok. Here is my world code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends greenfoot.World
{
    Counter counter;
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1);
        prepare();
    }
 
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Pacman pacman = new Pacman();
        addObject(pacman,754,91);
        Counter counter = new Counter();
        addObject(counter,728,38);
        Pellet pellet = new Pellet();
        addObject(pellet,647,95);
        Pellet pellet2 = new Pellet();
        addObject(pellet2,561,70);
        pellet2.setLocation(601,99);
        Pellet pellet3 = new Pellet();
        addObject(pellet3,531,88);
        pellet3.setLocation(540,94);
        Pellet pellet4 = new Pellet();
        addObject(pellet4,468,89);
        pellet4.setLocation(494,101);
    }
}
danpost danpost

2018/12/17

#
Will5230 wrote...
Ok. Here is my world code << Code Omitted >>
Bingo -- there it is. Remove the first word (Counter) from line 31.
Will5230 Will5230

2018/12/18

#
Thanks a lot!!
You need to login to post a reply.