I am trying to make an actor change to a specific image declared in an array when the Left and Right buttons are clicked. I have received the following error:
"
java.lang.NullPointerException: Filename must not be null.
at greenfoot.GreenfootImage.loadFile(GreenfootImage.java:293)
at greenfoot.GreenfootImage.<init>(GreenfootImage.java:108)
at greenfoot.Actor.setImage(Actor.java:439)
at spaceship.movement(spaceship.java:103) line 95 below
at spaceship.act(spaceship.java:33) line 25 below
at greenfoot.core.Simulation.actActor(Simulation.java:567)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
at greenfoot.core.Simulation.runContent(Simulation.java:193)
at greenfoot.core.Simulation.run(Simulation.java:183)
java.lang.NullPointerException: Filename must not be null.
at greenfoot.GreenfootImage.loadFile(GreenfootImage.java:293)
at greenfoot.GreenfootImage.<init>(GreenfootImage.java:108)
at greenfoot.Actor.setImage(Actor.java:439)
at spaceship.movement(spaceship.java:98) line 90 below
at spaceship.act(spaceship.java:33)
at greenfoot.core.Simulation.actActor(Simulation.java:567)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
at greenfoot.core.Simulation.runContent(Simulation.java:193)
at greenfoot.core.Simulation.run(Simulation.java:183)
"
My image files are named correctly I believe (ship0, ship1, ship2)
The code:
public class spaceship extends Actor { String [] shipImage = new String [3]; boolean activate = false; public void shipImage() { fillArray(); } public void fillArray() { for(int i=0; i<3; i++) { shipImage[i] = "ship"+i+".png"; } } /** * Act - do whatever the spaceship wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { youLose(); movement(); } public boolean canSee(Class otherActor)//Actor detects other actors { Actor actor = getOneObjectAtOffset(0,0,otherActor); if(actor != null) { return true; } else { return false; } } public void destroy(Class otherActor)//Actor deletes the actor from the map { Actor actor = getOneObjectAtOffset(0,0,otherActor); if(actor != null) { getWorld().removeObject(actor); } } public void youLose() { if(canSee(laser2.class)) { getWorld().removeObject(this); } } public void shipShoot() { laser1 laser1 = new laser1(); getWorld().addObject(laser1, getX(),getY()); } public void movement() { if (Greenfoot.isKeyDown("space")) { if (activate == false) { shipShoot(); activate = true; } } else { activate = false; } if (Greenfoot.isKeyDown("right")) //spaceship moves to the right { setLocation(getX()+3,getY()); setImage(shipImage[1]); } if (Greenfoot.isKeyDown("left")) //spacehship moves to the left { setLocation(getX()-3,getY()); setImage(shipImage[2]); } } }