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

2020/6/5

Filename null pointer error while using array

Mattzh Mattzh

2020/6/5

#
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]);
        }
    }
    
}
danpost danpost

2020/6/5

#
Where do you call the shipImage method from?
Mattzh Mattzh

2020/6/5

#
Just realized that I had not called the shipImage method... That fixed my problem. However I am having an issue with two other parts of my game as of now The first one: "java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:713) at greenfoot.Actor.getX(Actor.java:165) at spaceship.movement(spaceship.java:97) line 89 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) " Code for #1:
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();
       shipImage();
    }    
    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;
                setImage(shipImage[0]);
            }
        }
        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]);
        }
    }
    
}
    }
*I am also having the same issue with theleft movement The second one: " java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:713) at greenfoot.Actor.isAtEdge(Actor.java:262) at alien.act(alien.java:22) line 14 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) " Code for #2:
public class alien extends Actor
{
    public static int score = 0;
    public static int speed = 1;
    /**
     * Act - do whatever the alien wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       alienShoot();
       destroyAlien();
       move(1);
       if(isAtEdge())
       {
             turn(180);
       }
 
       
       
    }  
    
    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 destroyAlien()
    {
        if(canSee(laser1.class))
        {
            removeTouching(laser1.class);
            getWorld().removeObject(this);
            score++;
        }
    }
    private void alienShoot()
    {
        if (Greenfoot.getRandomNumber(3500)>=3499) //1 in 3500 chance that an alien will generate and shoot a laser
        {
            laser2 laser2 = new laser2();
            getWorld().addObject(laser2, getX(),getY());
        }
    }
    
}
danpost danpost

2020/6/5

#
You only need to fill the array (or, rather, call shipImage) once (when the spaceship object is created). By calling it from act, you are repeatedly filling the array. Add a constructor to the spaceship class and call it from there. Actually, just call fillArray from there. It seems a bit much to have a method call a single method in the same class when you can just call that second method. Moreover, you could just code the filling of the array in the constructor (so no shipImage or fillArray method). As far as the IllegalStateException error, just put the calling of youLose last in the act method. Same goes with the calling of destroyAlien in the alien class.
You need to login to post a reply.