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

2012/11/13

NullPointerException

BradH BradH

2012/11/13

#
this is the error i get when I try to run my game, I have good and bad knights each have to actors to switch between to give them an animated look import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Knight1 here. * * @author (your name) * @version (a version number or a date) */ public class Knight1 extends Actor {private int Walk = (6); /** * Act - do whatever the Knight1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(1); Walk -- ; if(Walk == 0) { getWorld().addObject(new Knight2(), getX(), getY()); getWorld().removeObject(this); } Actor BadKNight2 = (new BadKNight2()); if (BadKNight2 != null) { World world; world = getWorld(); world.removeObject(BadKNight2); return; } Actor BadKnight1 = (new BadKnight1()); if (BadKnight1 != null) { World world; world = getWorld(); world.removeObject(BadKnight1); return; } } } This is my code for one of my knights when walk = 0 that is when the image switches over to Knight two when this happens the illusion of walking is seen. I think that the problem has to do with the actors (like Knight1 and Knight2) Switching out all the time and it does not work with the worldremoveObject method but I am not sure. Any suggestions?
danpost danpost

2012/11/13

#
The reason it does not work is that once you remove 'this' from the world, the 'getWorld' method will return 'null'. Looking at your code, it appears that everything after that statement (getWorld().removeObject(this);) is useless nonesense (creating new objects and removing them from a world they are not in). Either remove or comment out everything after the removal statement or put a 'return;' statement immediately after it.
You need to login to post a reply.