I've got a class which features some ArrayList of BufferedImage, but I've got a serious problem. There is 2 possibilities :
-The ArrayList are all static, and so their getting methods are : this works fine, as the app is launching and the animation is running perfectly. But I can't have differents animations since there are statics.
-The ArrayList (and their getters) are not static : I get a NullPointerException when getDown() is called, which point at the precise moment where this one is called.
Before that, I used simple arrays and I believed that use ArrayLists would solve the problem, but there is no difference.
I don't understand why it's doing that, could you please help me on this matter ?
public class AnimUnit { private static final int width = 32, height = 32, nbframe = 4; private ArrayList<BufferedImage> down; private ArrayList<BufferedImage> up; private ArrayList<BufferedImage> right; private ArrayList<BufferedImage> left; private ArrayList<BufferedImage> idle; public AnimUnit(SpriteSheet sheet) { this.down = new ArrayList<BufferedImage>(); this.up = new ArrayList<BufferedImage>(); this.left = new ArrayList<BufferedImage>(); this.right = new ArrayList<BufferedImage>(); this.idle = new ArrayList<BufferedImage>(); for(int i = 0; i < nbframe; i++) down.add(sheet.crop((width*2)+2, (height*i)+i, width, height)); for(int i = 0; i < nbframe; i++) up.add(sheet.crop((width*3)+3, (height*i)+i, width, height)); for(int i = 0; i < nbframe; i++) left.add(sheet.crop((width)+1, (height*i)+i, width, height)); for(int i = 0; i < nbframe; i++) right.add(sheet.crop((width*4)+4, (height*i)+i, width, height)); for(int i = 1; i < nbframe; i++) idle.add(sheet.crop(0, (height*i)+i, width, height)); } public static int getWidth() { return width; } public static int getHeight() { return height; } public ArrayList<BufferedImage> getDown() { return down; } public ArrayList<BufferedImage> getUp() { return up; } public ArrayList<BufferedImage> getRight() { return right; } public ArrayList<BufferedImage> getLeft() { return left; } public ArrayList<BufferedImage> getIdle() { return idle; }
AnimUnit? In general,staticmethods/members should be avoided in object-oriented programming. You should be working with the object instances instead.