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

2015/6/18

Help with my Animation Class

miningmanna miningmanna

2015/6/18

#
I need help with my Animation Class it says NullPointer Exception in Animation at line 41. I dont understand why. im passing the right data types and every file is existing. My Animation Class:
import greenfoot.*;
import java.util.List;
import java.util.ArrayList;

public class Animation
{
    private static List<Animation> animations = new ArrayList();
    private static List<String> names = new ArrayList();
    
    private Content owner;
    private int frames;
    private int curFrame = 1;
    private List<GreenfootImage> images = new ArrayList();
    private List<Integer> frame = new ArrayList();
    
    public Animation(int frames)
    {
        this.frames = frames;
        add(this, Integer.toString(animations.size()+1));
    }
    
    public Animation(int frames, String name)
    {
        this.frames = frames;
        add(this, name);
    }
    
    public void addImage(GreenfootImage image, int actionFrame)
    {
        images.add(image);
        frame.add(actionFrame);
    }
    
    public void nextFrame()
    {
        int size = images.size();
        for(int i = 0; i<=size; i++)
        {
            if(curFrame == frame.get(i))
            {
                owner.setImage(images.get(i));
            }
        }
    }
    
    public void reset()
    {
        curFrame = 0;
        owner = null;
    }
    
    private void setOwner(Content owner)
    {
        this.owner = owner;
    }
    
    private static void add(Animation a, String name)
    {
        animations.add(a);
        names.add(name);
    }
    
    public static Animation getAnimation(String name)
    {
        int size = names.size();
        for(int i = 0; i<size; i++)
        {
            if(names.get(i) == name)
            {
                return animations.get(i);
            }
        }
        return null;
    }
    
    public static Animation setAnimation(Content owner, String name)
    {
        Animation a = getAnimation(name);
        a.setOwner(owner);
        return a;
    }
}
My Acting Class:
import greenfoot.*;

public class Content extends Actor
{   
    Animation curAnimation;
    
    public Content()
    {
        Animation a = new Animation(60, "bee");
        a.addImage(new GreenfootImage("bee.png"), 1);
        a.addImage(new GreenfootImage("bee2.png"), 31);
        curAnimation = a;
    }
    
    public void act()
    {
        curAnimation.nextFrame();
    }
    
    public void onClick()
    {
        
    }
    
    public void onHover()
    {
        
    }
}
JetLennit JetLennit

2015/6/18

#
Perhaps you should try changing nextFrame() to
 public void nextFrame()
    {
        int size = images.size() - 1;
        for(int i = 0; i<=size; i++)
        {
            if(curFrame == frame.get(i))
            {
                owner.setImage(images.get(i));
            }
        }
    }
wabuilderman wabuilderman

2015/6/18

#
^^ I agree. I think that the size() function returns the number of objects in the arraylist. i.e. if there is 1 item, it will return "1". To reference that object, you have to use get(0);
danpost danpost

2015/6/18

#
This is line 41:
owner.setImage(images.get(i));
If 'i' was a number more than the size of the list, you would get an 'IndexOfOfBounds' exception. If 'images' was not assigned a List object then you would indeed get a NullPointerException; however, that would have occurred on line 36 if that were the case (you get the 'size' of the List on that line -- so, 'images' must have been assigned a List object). That leaves 'owner'. I noticed that you have two places where the value of 'owner' could be set -- in the 'setAnimation' method and in the 'setOwner' method. I do not see where either of these methods are being called from.
miningmanna miningmanna

2015/6/19

#
Thanks guys i got it now :D i struggled with a proper animation class for quite a while but now i got one :D
miningmanna miningmanna

2015/6/19

#
There is actually still one thing, now i can only run one Actor at a time with the Animation
miningmanna miningmanna

2015/6/19

#
it would be nice if i could copy the Animation and run it for multiple Actors
miningmanna miningmanna

2015/6/19

#
nevermind made a cloneAnimation method :) Still thanks for your support: - danpost - wabuilderman - JetLennit :)
You need to login to post a reply.