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

2013/6/3

Null Pointer Exception

Student_Troy Student_Troy

2013/6/3

#
I am making a side-scrolling game and have run into a problem. The code is supposed to remove the object that the bullet intersects.
public void hit()
    {
        getWorld().addObject(this, getX()+45, getY()+14);
        List <Actor> objects = this.getIntersectingObjects(null);
        for (int i = 0; i < objects.size(); i++)
        {
            if (objects.get(i) instanceof Jumper && !(objects.get(i) instanceof Player))
            {
                this.getWorld().removeObject(objects.get(i));
                this.getWorld().removeObject(this);
                i--;
                count++;       // monster is killed, kill count +1
            }
        }
    }
The error that pops up is: java.lang.NullPointerException at Bullet.hit(Bullet.java:53) at Bullet.act(Bullet.java:30)
danpost danpost

2013/6/3

#
Remove line 11 from the code above (i--;). You may be removing an object from the world, but it is still in the list; so you do not need to decrease its value.
You need to login to post a reply.