【问题标题】:Unable to get a variable unless it's static [closed]除非它是静态的,否则无法获取变量[关闭]
【发布时间】:2017-07-28 22:15:28
【问题描述】:

我有一个类,其中包含一些 ArrayList 的 BufferedImage,但我遇到了一个严重的问题。有两种可能:

- ArrayList 都是静态的,因此它们的获取方法是:这工作正常,因为应用程序正在启动并且动画正在完美运行。但我不能有不同的动画,因为有静态。

-ArrayList(和它们的 getter)不是静态的:当 getDown() 被调用时,我得到一个 NullPointerException,它指向调用这个的精确时刻。

在此之前,我使用简单的数组,我认为使用 ArrayLists 可以解决问题,但没有区别。

我不明白为什么会这样,你能帮我解决这个问题吗?

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的客户端代码调用方法吗?通常,在面向对象编程中应避免使用static 方法/成员。您应该改为使用对象实例。
  • 我建议使用单例模式而不是静态方法来“随机”类方法访问。
  • 我从代码中删除了“静态”,因为似乎人们误解了我的观点。我希望我的代码与实例一起正常运行,但如果它们不是静态的,它会不断给我 NullPointerException,这对我来说很奇怪。我没有找到解决问题的方法...
  • 很多人考虑singletonsananti-pattern

标签: java arraylist nullpointerexception static bufferedimage


【解决方案1】:

目前,您的类中维护的属性都是静态的。您正在使用构造函数为它们分配值,这可能会误导您的类的用户,因为您的类没有非静态属性。如果你的构造函数没有被调用,那么它们就没有被初始化(并且在访问时会抛出空指针异常),但是除了静态方法之外什么都没有的构造对象是没有用的。

从您的所有属性和方法中删除“静态”一词,我认为它会像您希望和期望的那样工作。

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

【讨论】:

  • 我认为你误解了我的帖子。我知道我不能将这个类与静态 ListArray 一起正确使用,但是当我删除它们时会得到 NullPointerExceptions(当然,我在属性和 getter 之前删除了“静态”)
  • 您已经编辑了帖子中的代码,因此您应该验证您的情况是否发生了变化。当您的属性是静态的时,您可以在不初始化它们的情况下访问它们。现在它们是非静态的,除非你已经构建了你的对象(初始化它们),否则你不能尝试访问它们。
【解决方案2】:

从您的所有属性和方法中删除“静态”一词,并且每当您将某些内容初始化为 null 时,将其初始化为“”。

例子:

代替:

String xyz = null;

试试:

String xyz = "";

【讨论】:

  • 它不会改变任何东西,因为我从未将某些东西初始化为“null”或“”
【解决方案3】:

好吧,我刚刚做了一些测试,最终发现 NullPointerException 是关于 AnimUnit 类本身的一个实例,而不是 ArrayLists 之一。无论如何,感谢所有人,即使问题根本不在于数组

【讨论】:

  • 所以删除你无用的问题。
  • 对不起,即使这个问题是徒劳的,我也不能删除它,因为“很多人都投入了时间和精力来回答它”。
猜你喜欢
  • 2012-08-18
  • 1970-01-01
  • 1970-01-01
  • 2017-04-30
  • 1970-01-01
  • 2018-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多