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

2018/1/11

nullPointerException but I don't know why?

DrCrane DrCrane

2018/1/11

#
I'm trying to create a game for a project and I am getting a nullPointerException from the following code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * RPGBattleSystem
 */
public class RPGBattleSystem extends Actor
{
    public RPGBattleSystem()
    {
        clearRPGSystem();

        L2Battle l2battle = (L2Battle)getWorld();
        World world = getWorld();

        world.addObject(new RPGSystem(1), 300, 325);
    }    

    public void clearRPGSystem()
    {
        L2Battle l2battle = (L2Battle)getWorld();
        World world = getWorld();

        world.removeObjects(world.getObjects(RPGSystem.class)); //nullPointer

        System.out.print("Instances of RPGSystem.class: ");
        System.out.println(world.getObjects(RPGSystem.class).size()); //nullPointer
    }
}
I have noted in comments what lines give nullPointerExceptions. The RPGSystem.class which is referred to has the following code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * RPGSystem
 */
public class RPGSystem extends Actor
{
    private final int LAYOUT_WIDTH = 600;
    private final int LAYOUT_HEIGHT = 150;
    private final int LAYOUT_R = 118;
    private final int LAYOUT_G = 120;
    private final int LAYOUT_B = 122;
    private final int LAYOUT_T = 255;

    private final int Button_WIDTH = 200;
    private final int Button_HEIGHT = 50;
    private final int Button_R = 158;
    private final int Button_G = 160;
    private final int Button_B = 163;
    private final int Button_T = 255;

    protected int redCapHealthPoints = 100;
    protected int redCapDamageMultiplier = 1;
    
    
    public int cmd = 0;

    /**
     * Act - do whatever the RPGSystem wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public RPGSystem(int cmd)
    {
        this.cmd=cmd;
        cmdLine(cmd);
    }    

    public void act()
    {
        L2Battle l2battle = (L2Battle)getWorld();
        World world = getWorld();
        if (Greenfoot.isKeyDown("1"))
        {
            world.addObject(new RPGBattleSystem(), 300, 325);
            Greenfoot.delay(5);
        }
        if (Greenfoot.isKeyDown("2"))
        {
            //Nothing yet
        }
    }
    
    public void cmdLine(int cmd)
    {
        //Define world commands
        L2Battle l2battle = (L2Battle)getWorld();
        World world = getWorld();

        System.out.print("Calling RPGSystem function: ");
        System.out.println(cmd);
        if (cmd == 0)
        {
            System.out.println("Error. Tried to call a RPGSystem function 0: Does not exist.");
        }

        if (cmd == 1) //RPGSystem layout
        {
            GreenfootImage image = new GreenfootImage(LAYOUT_WIDTH, LAYOUT_HEIGHT); 

            image.setColor(new greenfoot.Color(LAYOUT_R,LAYOUT_G,LAYOUT_B,LAYOUT_T));
            image.fillRect(0, 0, LAYOUT_WIDTH, LAYOUT_HEIGHT );

            setImage(image);
        }

        if (cmd == 2) //RPGSystem Button-Battle
        {
            GreenfootImage image = new GreenfootImage(Button_WIDTH, Button_HEIGHT); 

            image.setColor(new greenfoot.Color(Button_R,Button_G,Button_B,Button_T));
            image.fillRect(0, 0, Button_WIDTH, Button_HEIGHT );

            Font font = image.getFont();
            font = font.deriveFont(26.0f);
            image.setFont(font);
            image.setColor(new Color(255, 255, 255));
            image.drawString("Battle [1]", 20, 35);

            setImage(image);
        }

        if (cmd == 3) //RPGSystem Button-Item
        {
            GreenfootImage image = new GreenfootImage(Button_WIDTH, Button_HEIGHT); 

            image.setColor(new greenfoot.Color(Button_R,Button_G,Button_B,Button_T));
            image.fillRect(0, 0, Button_WIDTH, Button_HEIGHT );

            Font font = image.getFont();
            font = font.deriveFont(26.0f);
            image.setFont(font);
            image.setColor(new Color(255, 255, 255));
            image.drawString("Items [2]", 20, 35);

            setImage(image);
        }
    }
}
I have no idea why I am getting a nullPointerException and I have not been able to find out why the removeObjects line in RPGBattleSystem is giving me a nullPointerException. Any help is greatly appreciated
DrCrane DrCrane

2018/1/11

#
I just figured out a way to go around this, and use the world class for removeObjects() instead. If anyone has anything that would help me put it in the actor class that would be greatly appreciated
Super_Hippo Super_Hippo

2018/1/11

#
You call the method from the constructor. The object is not in the world, so 'getWorld' returns null and not a reference to the world in which it maybe will be added later. You can pass a reference of the world to the constructor or you can use the 'addedToWorld' method which will be executed when the object is added to a world.
danpost danpost

2018/1/11

#
From where are you creating an RPGBattleSystem object? Maybe you can do something more there.
DrCrane DrCrane

2018/1/15

#
danpost wrote...
From where are you creating an RPGBattleSystem object? Maybe you can do something more there.
The RPGBattleSystem object is created when pressing the "1" key. The code for this is in the RPGSystem class
public void act()
    {
        L2Battle l2battle = (L2Battle)getWorld();
        World world = getWorld();
        if (Greenfoot.isKeyDown("1"))
        {
            world.addObject(new RPGBattleSystem(), 300, 325);
            Greenfoot.delay(5);
        }
        if (Greenfoot.isKeyDown("2"))
        {
            //Nothing yet
        }
    }
danpost danpost

2018/1/15

#
DrCrane wrote...
The RPGBattleSystem object is created when pressing the "1" key. The code for this is in the RPGSystem class < Code Omitted >
You could remove the RPGSystem object(s) here in this act method. Just do so after adding the RPGBattleSystem and add a return statement at the end of the inner block of code (within the 'if' block).
DrCrane DrCrane

2018/1/16

#
danpost wrote...
DrCrane wrote...
The RPGBattleSystem object is created when pressing the "1" key. The code for this is in the RPGSystem class < Code Omitted >
You could remove the RPGSystem object(s) here in this act method. Just do so after adding the RPGBattleSystem and add a return statement at the end of the inner block of code (within the 'if' block).
Could you give me an example? I don't think I understand you.
danpost danpost

2018/1/16

#
DrCrane wrote...
Could you give me an example?
if (Greenfoot.isKeyDown("1"))
{
    world.addObject(new RPGBattleSystem(), 300, 325);
    world.removeObjects(RPGSystem.class);
    Greenfoot.delay(5);
    return;
}
DrCrane DrCrane

2018/1/26

#
danpost wrote...
DrCrane wrote...
Could you give me an example?
if (Greenfoot.isKeyDown("1"))
{
    world.addObject(new RPGBattleSystem(), 300, 325);
    world.removeObjects(RPGSystem.class);
    Greenfoot.delay(5);
    return;
}
Sorry for the late reply, but thanks. That worked.
You need to login to post a reply.