Java 空指针异常

发布于 2024-08-21 20:15:10 字数 999 浏览 7 评论 0原文

我试图将我在网上找到的一个类用于动态整数数组,以适应动态“实体”数组,但现在我得到了“NullPointerException”。

引发异常的代码是:

public void initialize()
{
    buffer = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);
    Entities.put(Entities.getCurrentPos()+1, new Entity(100, 100, Color.green));
    Entities.put(Entities.getCurrentPos()+1, new Entity(400, 400, Color.blue));
}

DynArrayEntities 类的相关部分:

...

private Entity[] data;  // An array to hold the data.
private int currentpos = 0;

...

public void put(int position, Entity value) {

    if (position >= data.length) {

        int newSize = 2 * data.length;
            if (position >= newSize)
                newSize = 2 * position;
        Entity[] newData = new Entity[newSize];
        System.arraycopy(data, 0, newData, 0, data.length);
        data = newData;
    }

    data[position] = value;
    currentpos++;

}

....

public int getCurrentPos() {
    return currentpos;
}

提前感谢您的帮助!

I attempted to adapt a class I had found on the web for a dynamic array of ints for a dynamic array of "Entities," but now I am getting a "NullPointerException."

The code raising the exception is:

public void initialize()
{
    buffer = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);
    Entities.put(Entities.getCurrentPos()+1, new Entity(100, 100, Color.green));
    Entities.put(Entities.getCurrentPos()+1, new Entity(400, 400, Color.blue));
}

The relevant parts of DynArrayEntities class:

...

private Entity[] data;  // An array to hold the data.
private int currentpos = 0;

...

public void put(int position, Entity value) {

    if (position >= data.length) {

        int newSize = 2 * data.length;
            if (position >= newSize)
                newSize = 2 * position;
        Entity[] newData = new Entity[newSize];
        System.arraycopy(data, 0, newData, 0, data.length);
        data = newData;
    }

    data[position] = value;
    currentpos++;

}

....

public int getCurrentPos() {
    return currentpos;
}

Thanks in advance for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丘比特射中我 2024-08-28 20:15:10
...

private Entity[] data= new Entity[0];  // Or some value > 0
...

否则,第一次在方法中访问数据时数据为空。

...

private Entity[] data= new Entity[0];  // Or some value > 0
...

otherwise data is null the first time you access it in the method.

剑心龙吟 2024-08-28 20:15:10

所做的事情

position >= data.length

您在初始化data之前

you're doing

position >= data.length

before initializing data

殤城〤 2024-08-28 20:15:10

(Entities 是一个字段吗?在这种情况下,您应该将其称为实体。请参阅 http://java.sun .com/docs/codeconv/

您应该准确地指出 NPE 是在哪一行抛出的。如果它位于initialize() 方法的第二行,则Entities 字段可能为空。如果是在 put() 方法中,那么可能是因为数据字段为空。

(Is Entities a field? In that case you should call it entities. See http://java.sun.com/docs/codeconv/)

You should tell exactly that on which line the NPE is thrown. If it's in the initialize() method's second line, then probably the Entities field is null. If it's in the put() method, then probably it's because the data field is null.

碍人泪离人颜 2024-08-28 20:15:10

您这样做只是作为学习练习吗?如果没有,为什么不使用 java .util.Vector,它为任何对象提供动态数组?

Are you doing this just as a learning exercise? If not, why not use java.util.Vector, which provides a dynamic array for any Object?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文