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

2014/1/16

what?

deathburrito16 deathburrito16

2014/1/16

#
when I try to implement the code from the access tutorial, this comes up in the terminal window. java.lang.NullPointerException at Bullet.addScore(Bullet.java:37) at Bullet.act(Bullet.java:29) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) here is the code in the world:
private Counter theCounter;

    public Counter getCounter()
    {
        return theCounter;
    }
here is the code in the bullet: move(50); Actor enemy; enemy = getOneObjectAtOffset(0, 0, Enemy.class); if (enemy != null) { World world; world = getWorld(); world.removeObject(enemy); Deadenemy deadenemy = new Deadenemy(); getWorld().addObject(deadenemy, getX(), getY()); addScore(); getWorld().removeObject(this); } public void addScore() { Space spaceWorld = (Space) getWorld(); Counter counter = spaceWorld.getCounter(); counter.bumpCount(5); }
danpost danpost

2014/1/16

#
The problem is probably in your world class.
deathburrito16 deathburrito16

2014/1/16

#
what would that problem be?
danpost danpost

2014/1/17

#
If your world class code was posted, someone might be able to help.
davmac davmac

2014/1/17

#
what would that problem be?
The problem is that you are calling a method (or variable) through a null reference. Just a guess from looking at the code you posted: Perhaps you are never setting 'theCounter' in your world to anything?
deathburrito16 deathburrito16

2014/1/17

#
damvac, the code is exactly the same as in the tutorial. I'll post the code here, just because.
     */
    public Space()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1050, 550, 1); 

        prepare();
    }
    public Counter getCounter()
    {
        return theCounter;
    }
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {

        Player player = new Player();
        addObject(player, 507, 339);

        Spawner spawner = new Spawner();
        addObject(spawner, 1042, 298);
        removeObject(spawner);
        Spawner spawner2 = new Spawner();
        addObject(spawner2, 1022, 308);
        Grunt grunt = new Grunt();
        addObject(grunt, 889, 146);
        Grunt grunt2 = new Grunt();
        addObject(grunt2, 810, 404);
        player.setLocation(492, 467);
        grunt2.setLocation(938, 473);
        removeObject(grunt);
        player.setLocation(446, 290);
        removeObject(spawner2);
        Flamer flamer = new Flamer();
        addObject(flamer, 977, 97);
        Zook zook = new Zook();
        addObject(zook, 945, 313);
        Redicle redicle = new Redicle();
        addObject(redicle, 773, 303);
        Spawner spawner3 = new Spawner();
        addObject(spawner3, 1031, 315);
        Counter counter = new Counter();
        addObject(counter, 41, 533);
        counter.setLocation(34, 521);
    }
danpost danpost

2014/1/17

#
So, you have declared an instance field with:
Counter theCounter; // with possible ' = null'
and you have a 'get' method to return the counter object (lines 9 through 12). But, where is it being set? You have a 'new Counter()' being created at line 45 and set to a local field you called 'counter' and then added that counter into the world on the next line; but, 'theCounter' is still 'null'.
deathburrito16 deathburrito16

2014/1/18

#
I just don't understand. I mostly just copied the code from the tutorial.
danpost danpost

2014/1/18

#
Change lines 45 through 47 to the following:
theCounter = new Counter(); // assigns 'theCounter' a new Counter object
addObject(theCounter, 34, 521); // adds the Counter object held by 'theCounter' field to the world
I took the liberty of adding the object at the location your line 47 placed it at and removed that line from the code.
deathburrito16 deathburrito16

2014/1/18

#
thanks, danpost.
You need to login to post a reply.