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

2018/4/23

Greenfoot Terminal Window: NullPointerException

Lavenger Lavenger

2018/4/23

#
It would've seem that my code has problems running due to the NullPointerException error but i have no idea what the problems seems to be, my code has no syntax error but it won't run. Would anyone tell me why is that happening to me? This is my world code which also has the error(Line 17):
import greenfoot.*;
public class Minigame2 extends World
{
    public int Score, Score2;
    int RandX = (Greenfoot.getRandomNumber(601));
    int RandY = (Greenfoot.getRandomNumber(401));
    public Actor Scoreboard = new SimpleActor(), Scoreboard2 = new SimpleActor();
    public Minigame2()
    {
        super(600, 400, 1);
        GreenfootImage bg = new GreenfootImage("Arcade.jpg");
        setBackground(bg);
        adjustScore(0);
        adjustScore2(0);
        addObject(Scoreboard, 300, 200);
        addObject(Scoreboard2, 300, 200);
        addObject(new MiniSylphynford(), RandX, RandY);
        addObject(new MiniUmaru(), RandX, RandY);
        for (int i= 0; i < 21;)
            {
            int randX = 0, randY = 0;
            while (randX == 0 || !getObjectsAt(randX, randY, Actor.class).isEmpty())
            {
                randX = (Greenfoot.getRandomNumber(601));
                randY = (Greenfoot.getRandomNumber(401));
            }
            addObject(new ArcadeTicket(), randX, randY);
            i++;
            }
        for (int i= 0; i < 11;)
            {
            int randX = 0, randY = 0;
            while (randX == 0 || !getObjectsAt(randX, randY, Actor.class).isEmpty())
            {
                randX = (Greenfoot.getRandomNumber(601));
                randY = (Greenfoot.getRandomNumber(401));
            }
            addObject(new RandomPaper(), randX, randY);
            i++;
            }
    }
    public void adjustScore(float adjustment)
    {
        Score += adjustment;
        Score = (int)Score;
        Scoreboard.setImage(new GreenfootImage("Yen: "+Score, 20, Color.BLACK, new Color(0, 0, 0, 0)));
    }
    public void adjustScore2(float adjustment)
    {
        Score2 += adjustment;
        Score2 = (int)Score2;
        Scoreboard2.setImage(new GreenfootImage("Yen: "+Score2, 20, Color.BLACK, new Color(0, 0, 0, 0)));
    }
}
This is the actor that's receiveing the error(Line 6):
import greenfoot.*;
public class MiniSylphynford extends Actor
{
    public Actor ArcadeTicket, RandomPaper;
    public Minigame2 mg2 = (Minigame2)getWorld();
    int Score = mg2.Score;
    public void act() 
    {
        ArcadeTicket = getOneObjectAtOffset(0,0,ArcadeTicket.class);
        RandomPaper = getOneObjectAtOffset(0,0,RandomPaper.class);
        if(Greenfoot.isKeyDown("left")) {turn(-5);}
        if(Greenfoot.isKeyDown("right")) {turn(5);}
        if(Greenfoot.isKeyDown("up")) {move(5);}
        if(Greenfoot.isKeyDown("down")) {move(-5);}
    }
    public void GainScore() 
    {
        if (ArcadeTicket != null)
        {
            mg2.removeObject(ArcadeTicket);
            Score += 1;
        }
        if (RandomPaper != null)
        {
            mg2.removeObject(RandomPaper);
            Score -= 2;
        }
        float ScoreAdjustment = 0; // every cycle this value will pe deposited onto the world's yen value
        ScoreAdjustment += Score;
        mg2.adjustScore(ScoreAdjustment);
        // When buying something, only subtract the price from AGold!
    }
}
danpost danpost

2018/4/23

#
Line 5 in the MiniSylphynford class sets mg2 to null (the actor is not yet in any world at the time it is being created). The order of execution of line 17 in the Minigame2 class is, first, a new MiniSylpynford object is created (in doing so, what happens is that the fields are set up and set to any values assigned to them, including those in any super-classes; then, the constructor is executed); then, the object is added into the world.
Lavenger Lavenger

2018/4/24

#
danpost wrote...
Line 5 in the MiniSylphynford class sets mg2 to null (the actor is not yet in any world at the time it is being created).
So if i want it to work, do I add the MiniSylphynford class into the world first?
danpost danpost

2018/4/24

#
Lavenger wrote...
So if i want it to work, do I add the MiniSylphynford class into the world first?
Once you do, the getWorld method will return the Minigame2 world object that it was placed into instead of null.
Lavenger Lavenger

2018/4/24

#
danpost wrote...
Lavenger wrote...
So if i want it to work, do I add the MiniSylphynford class into the world first?
Once you do, the getWorld method will return the Minigame2 world object that it was placed into instead of null.
hmm but i have a problem here if i want to do that... the world that the MiniSylphynford is supposed to be in won't appear until an event triggers in the main world... so what do i do then?
danpost danpost

2018/4/24

#
Mainly, in the MiniSylphynford class, remove lines 5 and 6. and change all 'mg2' to 'getWorld()'. Then, lines 21 and 26 can both be something like the following:
((Minigame2)getWorld()).adjustScore(1f);
with lines 28 through 30 removed.
Lavenger Lavenger

2018/4/28

#
It's now stuck trying to build the world. There is no error but once I try to open that world, I can't open any other world since it's trying to build it (although it's stuck).
danpost danpost

2018/4/28

#
Lavenger wrote...
It's now stuck trying to build the world. There is no error but once I try to open that world, I can't open any other world since it's trying to build it (although it's stuck).
Show your changes. Post the class code for review.
Lavenger Lavenger

2018/4/28

#
danpost wrote...
Lavenger wrote...
It's now stuck trying to build the world. There is no error but once I try to open that world, I can't open any other world since it's trying to build it (although it's stuck).
Show your changes. Post the class code for review.
Here's the World class code:
import greenfoot.*;
public class Minigame2 extends World
{
    public int Score, Score2;
    int RandX = (Greenfoot.getRandomNumber(601));
    int RandY = (Greenfoot.getRandomNumber(401));
    public Actor Scoreboard = new SimpleActor(), Scoreboard2 = new SimpleActor();
    public Minigame2()
    {
        super(600, 400, 1);
        GreenfootImage bg = new GreenfootImage("Arcade.jpg");
        setBackground(bg);
        adjustScore(0);
        adjustScore2(0);
        addObject(Scoreboard, 300, 200);
        addObject(Scoreboard2, 300, 200);
        addObject(new MiniSylphynford(), RandX, RandY);
        addObject(new MiniUmaru(), RandX, RandY);
        for (int i= 0; i < 10;)
            {
            int randX = 0, randY = 0;
            while (randX == 0 || !getObjectsAt(randX, randY, Actor.class).isEmpty())
            {
                randX = (Greenfoot.getRandomNumber(601));
                randY = (Greenfoot.getRandomNumber(401));
            }
            addObject(new ArcadeTicket(), randX, randY);
            i++;
            }
        for (int i= 0; i < 4;)
            {
            int randX = 0, randY = 0;
            while (randX == 0 || !getObjectsAt(randX, randY, Actor.class).isEmpty())
            {
                randX = (Greenfoot.getRandomNumber(601));
                randY = (Greenfoot.getRandomNumber(401));
            }
            addObject(new RandomPaper(), randX, randY);
            i++;
            }
    }
    public void adjustScore(float adjustment)
    {
        Score += adjustment;
        Score = (int)Score;
        Scoreboard.setImage(new GreenfootImage("Yen: "+Score, 20, Color.BLACK, new Color(0, 0, 0, 0)));
    }
    public void adjustScore2(float adjustment)
    {
        Score2 += adjustment;
        Score2 = (int)Score2;
        Scoreboard2.setImage(new GreenfootImage("Yen: "+Score2, 20, Color.BLACK, new Color(0, 0, 0, 0)));
    }
}
and here's the Actor class code:
import greenfoot.*;
public class MiniSylphynford extends Actor
{
    public Actor ArcadeTicket, RandomPaper;
    public void act() 
    {
        ArcadeTicket = getOneObjectAtOffset(0,0,ArcadeTicket.class);
        RandomPaper = getOneObjectAtOffset(0,0,RandomPaper.class);
        if(Greenfoot.isKeyDown("left")) {turn(-5);}
        if(Greenfoot.isKeyDown("right")) {turn(5);}
        if(Greenfoot.isKeyDown("up")) {move(5);}
        if(Greenfoot.isKeyDown("down")) {move(-5);}
    }
    public void GainScore() 
    {
        if (ArcadeTicket != null)
        {
            getWorld().removeObject(ArcadeTicket);
            ((Minigame2)getWorld()).adjustScore(1f);
        }
        if (RandomPaper != null)
        {
            getWorld().removeObject(RandomPaper);
            ((Minigame2)getWorld()).adjustScore(-2f);
        }
    }
}
danpost danpost

2018/4/28

#
Lavenger wrote...
It's now stuck trying to build the world. There is no error but once I try to open that world, I can't open any other world since it's trying to build it (although it's stuck).
Well, I do not think it is from the changes that were made. Where is the GainScore method being called from.
Lavenger Lavenger

2018/4/28

#
danpost wrote...
Where is the GainScore method being called from.
oh I forgot to add it into the act() method in the actor
Lavenger Lavenger

2018/4/28

#
if it helps figuring out why it wont start: I opened the debugger as the world is being created and it says this under the instance variables in the first call sequence(java.util.HashMap$HashIterator.<init>): HashMap.Node next = null HashMap.Node current = null int expectedModCount = 1 int index = 12
You need to login to post a reply.