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

2018/3/16

Why does this Keep on giving me a null pointer exception?

itssemu itssemu

2018/3/16

#
public class Level1 extends World
{
    private GreenfootImage mouthOpened = new GreenfootImage("Transparent Jordan mouth opened.png");//creates a variable that the Jordan mouth opened picture is stored in.
    private GreenfootImage mouthClosed = new GreenfootImage("Transparent Jordan mouth closed.png");//creates a variable that the Jordan mouth closed picture is stored in.
    private Jordan[] jordans = new Jordan[getHeight()/41];//defines a variable with multiple actors inside of it.
    private int JCurseCounter = 0;//sets a counter to zero at the start of the world.
    private boolean changingWorld = false;
    public Level1()
    {
        super(600, 400, 1); //Creates a new world with 600 pixels on the X axis and 400 pixels on the Y axis with a cell size of 1x1 pixels.
        Alfie alfie = new Alfie();//defines alfie as a new actor so that it can be referenced from another place in the code
        addObject(alfie, 20, getHeight()/2); //Adds an actor alfie into the world
        for(int i = 1; i < jordans.length; i++)//Creates a for loop to add the jordans to the world and all as different names although they are added to the world in the same loop.
        {
            jordans[i] = new Jordan();
            addObject(jordans[i], getWidth() - 25, i * 45);
        }
    }

    public void act() // makes a void that constantly runs in the world
    {
        for(int noj = 1; noj <= jordans.length; noj++)
        {
            JCurseCounter++;
            if(Greenfoot.getRandomNumber(jordans.length) == noj && changingWorld == false)
            {
                if(JCurseCounter >= 700)
                {
                    JCurse jcurse = new JCurse();
                    addObject(jcurse, 575, noj*47);
                    JCurseCounter = 0;
                }
                if(JCurseCounter < 400 && JCurseCounter > 200)
                {
                    jordans[noj].setImage(mouthClosed);
                }
            }
        }
    }
}
Super_Hippo Super_Hippo

2018/3/16

#
Please tell us at what line the error occurs. When I run this code, it doesn't give me a nullpointer exception. Note that arrays indices start at 0 and not at 1. Looking at lines 13 and 22, you once use < and once <=. <= could give you another error if it tries to find Jordans 9 because only 0..8 exist (while 0 is null in your case).
itssemu itssemu

2018/3/16

#
bit wierd but it seems to have fixed itself? idk it was giving me errors in line 35.
itssemu itssemu

2018/3/16

#
bit more code now but for anyone who wants to reference from this, it worked for me
public class Level1 extends World
{
    private GreenfootImage mouthOpened = new GreenfootImage("Transparent Jordan mouth opened.png");//creates a variable that the Jordan mouth opened picture is stored in.
    private GreenfootImage mouthClosed = new GreenfootImage("Transparent Jordan mouth closed.png");//creates a variable that the Jordan mouth closed picture is stored in.
    private Lives[] lives = new Lives[3];
    public static int Lives;
    public static int Score;
    private Jordan[] jordans = new Jordan[getHeight()/41];//defines a variable with multiple actors inside of it.
    private int JCurseCounter = 0;//sets a counter to zero at the start of the world.
    private boolean changingWorld = false;
    public Level1()
    {
        super(600, 400, 1); //Creates a new world with 600 pixels on the X axis and 400 pixels on the Y axis with a cell size of 1x1 pixels.
        Alfie alfie = new Alfie();//defines alfie as a new actor so that it can be referenced from another place in the code
        addObject(alfie, 20, getHeight()/2); //Adds an actor alfie into the world
        Score score = new Score();//defines score as a new variable with the actor score stored inside it
        addObject(score, 69, 13);//adds that variable that was defined before to the world
        Timer timer = new Timer();//defines timer as a new variable with the actor Timer stored in it
        addObject(timer, getWidth()/2, 10);//adds that variable to the world
        BeforeText beforetext = new BeforeText();//defines before text as a new variable with the actor BeforeText stored in it
        addObject(beforetext, getWidth()/2, getHeight()/2);//adds the objectBefore text in the middle of the world
        Lives = 3;//resets the static variable lives each time the world is reset
        Score = 0;//resets the static variable score each time the world is reset
        for(int i = 1; i < jordans.length; i++)//Creates a for loop to add the jordans to the world and all as different names although they are added to the world in the same loop.
        {
            jordans[i] = new Jordan();
            addObject(jordans[i], getWidth() - 25, i * 45);
        }
        for(int i = 0; i < lives.length; i++)//Creates a for loop that adds the hearts to the world
        {
            lives[i] = new Lives();//defines a new Lives class ass an array variable
            addObject(lives[i], i * 25 + getWidth()-(2*25) - 12, 10);//adds a new variable to the world depending on the worlds width
        }
    }
Juno Juno

2018/3/16

#
i was literally going to ask you how to do this but i guess it's answered :) - a guy in your class
You need to login to post a reply.