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

2015/5/18

Having a major problem with my game code, please help.

Raisoren Raisoren

2015/5/18

#
I used Danpost's "Platformer Tutorial" as a template/guide to making my game and have come across a terminal window error. java.lang.NullPointerException at greenfoot.World.addObject(World.java:392) at Level.<init>(Level.java:27) at Plaza.<init>(Plaza.java:4) The problems appear to be here: Level:
public abstract class Level extends World
{
    String[] map;
    public Level()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        setFields();
        for (int i=0; i<map.length; i++) for (int j=0; j<map[i].length(); j++)
        {
            int kind = "cpge".indexOf(""+map[i].charAt(j));
            if (kind < 0) continue;
            Actor actor = null;
            if (kind == 5) actor = new Player();
            if (kind == 2) actor = new Bricks ();
            if (kind == 4) actor = new Gunmen();
            if (kind == 0) actor = new ExitGate();
            addObject(actor, 16+j*32, 16+i*32 );
        }      
    }
    
And here: public class Plaza extends Level { /** MAP KEY */ // c = player p = bricks // g = gunmen e = exit public void setFields() { map = new String { " g e ", " g g ", " g ", " ppppp ppppp ", " ppppp ppppp ", " ppppp ", " g ", " ", " ppppp ", " g ppppp ppppp ", " g ", " g g ", " ppppp ppppp ppppp ", " ", " ", " ppppp ", " ppppp ", " g ", " ", " ", " ppppp c ppppp ", " g g ", " ", " ", "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" }; Any advice would be greatly appreciated, thanks in advance.
Super_Hippo Super_Hippo

2015/5/18

#
int kind = "cpge".indexOf(""+map[i].charAt(j));
With this, 'kind' can be -1, 0, 1, 2, or 3. So 'actor' can still be null when trying to add it to the world. Try to use 0, 1, 2, 3 instead of 5, 2, 4, 0 in the if-conditions in lines 14-17.
Raisoren Raisoren

2015/5/18

#
Sorry for the string map being screwed up, copy/paste can be a pain sometimes, i've tried to alter what Super_Hippo has pointed out and still gettiing the terminal error. The problems being highlighted are: public class Plaza extends Level from the "Plaza" subclass and addObject(actor, 16+j*32, 16+i*32 ); from the "Level" subclass. both compile well and i'm at a loss as to why this error is occuring.
danpost danpost

2015/5/18

#
Maybe if you show the entre Plaza class code, we might see a problem (starting at line 1).
Raisoren Raisoren

2015/5/18

#
I thought i had, here it is again. import greenfoot.Greenfoot; public class Plaza extends Level { /** MAP KEY */ // c = player p = bricks // g = gunmen e = exit public void setFields() { map = new String { " g e ", " g g ", " g ", " ppppp ppppp ", " ppppp ppppp ", " ppppp ", " g g ", " ", " g ppppp ", " ppppp ppppp ", " g ", " g ", " ppppp g ppppp ppppp ", " ", " ", " ppppp ", " ppppp ", " g ", " ", " ", " ppppp c ppppp ", " g g ", " ", " ", "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" }; } } Again, string map screwed up, but hopefully you get the gist of my problem.
Raisoren Raisoren

2015/5/18

#
UPDATE: The problem has been fixed, thanks all for replying. There was an extra letter in the "Level" subclass that didn't belong. Sorted now.
You need to login to post a reply.