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

2016/6/9

NullPointerException That I Really Need Help With

mreed7231 mreed7231

2016/6/9

#
I am making a shooter kind of game, and I have monsters shooting bullets towards the players... However, greenfoot never seems to run my constructor and the world for mBullet always ends up as null I have no idea why it isn't working. This is the code for my mBullet class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
 * The bullet of monsters
 * 
 * @mreed7231
 * 
 */
public class mBullet extends SmoothMover
{
    private World bulletWorld;
    private int timer;
    private int done = 0;
    int playerX;
    int playerY;
    public mBullet(World shooterWorld)
    {
        System.out.println("KITTEN");
        bulletWorld = shooterWorld;
        System.out.println("**"+bulletWorld);
    }
    {
        System.out.println("////"+bulletWorld);
        List Players = bulletWorld.getObjects(Player.class);
        if(Players.size() != 0)
        {
            Actor player = (Actor)bulletWorld.getObjects(Player.class).get(0);
            playerX = player.getX();
            playerY = player.getY(); 
        }
        if (done == 0)
        {
            turnTowards(playerX, playerY);
            done++;
        }
        move(0.5);
        if (timer == 0)
            timer = 600;
       if (timer > 0)
            {
                timer--;
                if ((this != null) && isAtEdge())
                {
                    getWorld().removeObject(this);
                }
                else if (timer == 0 && (this != null))
                {
                    getWorld().removeObject(this);
                }
            }
    }    
}
and here's the code for my monster class at the moment
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * 
 * 
 * @mreed7231
 */
public class Monster extends SmoothMover
{
    private static int hp;
    private static int lvl;
    private static int xp;
    private static int dex;

    public Monster()
    {
        hp = 300;
        lvl = 5;
        dex = 100;
        xp = 0;
    }

    private double time;
    private double time2;
    private double time3;
    //For Motion
    private GreenfootImage left = new GreenfootImage("MonsterOtherSide.gif");
    private GreenfootImage right= new GreenfootImage("MonsterSide.gif");
    private GreenfootImage up= new GreenfootImage("MonsterBack.gif");
    private GreenfootImage down= new GreenfootImage("MonsterForward.gif");
    //For Dexterity
    long lastAddedDex = System.currentTimeMillis();
    boolean inRange = false;
    Class Player;

    public void act() 
    {
        if (this.getObjectsInRange(15, Player) != null)
            inRange = true;
        if (inRange)
        {
            long curTime = System.currentTimeMillis();
            double waitTime = (60/(double)dex) * 1000;
            if (curTime >= (lastAddedDex + waitTime))
                {
                    shoot();
                    lastAddedDex  = curTime;
                }
        }
    }    

    public static void changehp(int value)
    {
        hp = hp + value;
    }
    
    public void shoot()
    {
        System.out.println(":-)"+getWorld()+" " +getX()+" " +getY());
        getWorld().addObject(new mBullet(getWorld()), getX(), getY());
    }
    
    public void getsHit()
    {
        changehp(-25);
    }
    
    public static int gethp()
    {
        return hp;
    }

    public static int getLvl()
    {
        return lvl;
    }
}
If any of you guys can help out it'd be a major life saver.
danpost danpost

2016/6/10

#
There are several issues that immediately come to my attention. (1) 'this' will never be 'null'; so, any checks for that can be removed (2) the fields 'hp', 'dex' and 'xp' (at least) should not be 'static' as each instance of the Monster class should have its own values for these fields (3) the block of code starting at line 22 of the mBullet class is missing its identifying code; should probably start with 'public void act()'. (4) methods that adjust the value of any field where 'static' was removed should also have their 'static' modifier removed ('changehp' and 'gethp').
danpost danpost

2016/6/10

#
Lines 37 through 51 of the mBullet class can be simplified to this:
timer++;
if (timer == 600 || isAtEdge())
    getWorld().removeObject(this);
danpost danpost

2016/6/10

#
danpost wrote...
(2) the fields 'hp', 'dex' and 'xp' (at least) should not be 'static' as each instance of the Monster class should have its own values for these fields
I am sure about the 'hp' field, here. As far as I can tell the 'xp' field is not being used; so, it can actually be removed at present. The 'dex' field I cannot say for sure one way or the other. I see where it is used and I do not see where its value would be changing and cannot truly tell if all monsters will have the same value for this field or not. However, 'xp' is usually used to represent 'experience points' and 'dex' is usually used to represent 'dexterity'. There are both states that belong to the individual monster and the monsters may or may not have different values for their corresponding states; so, they should be 'non-static' fields if I am correct.
mreed7231 mreed7231

2016/6/10

#
danpost wrote...
(3) the block of code starting at line 22 of the mBullet class is missing its identifying code; should probably start with 'public void act()'.
THANKS FOR THE HELP that was the main issue causing my error and I'll go in and fix up that other stuff too.
You need to login to post a reply.