Unable to get a variable unless it's static

244
July 29, 2017, at 2:18 PM

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;
}   
Answer 1

Currently, the properties maintained in your class are all static. You're using a constructor to assign values to them, which could be misleading to user of your class since your class has no non-static properties. If your constructor isn't called, then they're not initialized (and will throw null pointer exceptions when accessed), but a constructed object of nothing but static methods is kind of useless.

Remove the word "static" from all your properties and methods, and I think it will work like you'd want and expect.

AnimUnit animUnitA=new animUnit(spriteSheetA);
AnimUnit animUnitB=new animUnit(spriteSheetB);
ArrayList<BufferedImage> downA=animUnitA.getDown();
ArrayList<BufferedImage> downB=animUnitB.getDown();
Answer 2

Remove the word "static" from all your properties and methods, and whenever you initialize something to null initialize it to "" instead.

Example:

Instead of:

String xyz = null;

Try:

String xyz = "";
Answer 3

Ok I just made some tests and fianlly found out that the NullPointerException was about an instance of the AnimUnit class itself, not one the ArrayLists. Thanks to all anyway, even if the problem wasn't about the arrays at all

Rent Charter Buses Company
READ ALSO
Separate Table vs Extra Columns in JPA/Hibernate

Separate Table vs Extra Columns in JPA/Hibernate

I am wondering about best practices in database design with Hibernate

283
How to get URL of REST endpoint?

How to get URL of REST endpoint?

Can someone explain why this URL returns 404?

389
How to load tree/indexes from .yaml file in java? [on hold]

How to load tree/indexes from .yaml file in java? [on hold]

How to load tree/indexes fromyaml file in java? I want to grap all this values from

430
Java code will not print out answer or read int properly [duplicate]

Java code will not print out answer or read int properly [duplicate]

This question already has an answer here:

274