从类数组调用方法会给出 NullPointerException

发布于 2024-12-29 12:28:06 字数 3399 浏览 2 评论 0原文

我一直在寻找这个问题很多,但找不到解决方案。我正在尝试构建一个迷你游戏,并且我有一种创建平台的方法。我有一个包含每个平台参数的类,并且我创建了一个类数组,这样我就可以同时拥有多个平台。

问题:当我尝试通过发送我想要的参数来调用构建平台的方法时,它给了我一个NullPointerException。该方法之前可以工作,但所有内容都是静态的,因此我无法拥有该类的多个实例,现在我从平台类中删除了静态字段,每次调用时它都会给我 NullPointerException 。方法。

我复制了出现错误的代码部分,错误如下:

public static void main(String[] args) {
        Game ex = new Game();
        new Thread(ex).start();
    }

在 Game 类中:

public Load_Stage load = new Load_Stage();
public Game() {
        -other variables initializatin-
        Initialize_Items();
        load.Stage_1(); // <--- problem this way

在 Load_Stage 类中:

public class Load_Stage {
    public Platforms plat = new Platforms();

    public void Stage_1(){    
        Stage_Builder.Build_Platform(200, 500, 300, plat.platform1);
        Stage_Builder.Build_Platform(100, 200, 100, plat.platform1);
    }

}

在 Stage_Builder 类中:

public class Stage_Builder {

    public static final int max_platforms = 10;
    public static Platform_1[] p1 = new Platform_1[max_platforms];
    public static boolean[] platform_on = new boolean[max_platforms];    

    public Stage_Builder() {
        for (int c = 0; c < platform_on.length; c++) {
            platform_on[c] = false;
        }
    }
    public static void Build_Platform(int x, int y, int width, ImageIcon[] type) { // BUILDS A PLATFORM

        for (int b = 0; b < max_platforms; b++) {
            if (platform_on[b] == false) { 
                p1[b].Construct(x, y, width, type); // <-- NullPointerException here
                platform_on[b] = true; 
                break;
            }
        }
    }
}

预先感谢。

编辑:这是 Platform_1 类(抱歉忘记了):

public class Platform_1 {

    private int platform_begin_width = 30;
    private int platform_middle_width = 20;
    public int blocks_number = 0;
    public ImageIcon[] platform_floors = new ImageIcon[500];
    private int current_width = 0;
    public int [] platform_x = new int [500];
    public int platform_y = 0;
    public int platform_width = 0;

    public void Construct(int x, int y, int width, ImageIcon [] type) {        
        platform_width = width;
        platform_y = y;
        for (int c = 0; current_width <= platform_width; c++) { 
            if (c == 0) {
                platform_x[c] = x;
                platform_floors[c] = type[0];
                current_width += platform_begin_width;
            } else if ((current_width + platform_middle_width) > platform_width) {
                platform_floors[c] = type[2];
                blocks_number = c + 1;
                platform_x[c] = current_width + x;
                current_width += platform_middle_width;
            } else {
                platform_floors[c] = type[1];
                platform_x[c] = current_width + x;
                current_width += platform_middle_width;
            }
        }        
    }
}

还有 Platforms 类:

public class Platforms {

    public ImageIcon[] platform1 = {new ImageIcon("Resources/Sprites/Stage_Objects/Platform1/begin.png"),
        new ImageIcon("Resources/Sprites/Stage_Objects/Platform1/middle.png"),
        new ImageIcon("Resources/Sprites/Stage_Objects/Platform1/end.png")};
}

I've been searching a lot for this problem and I can't find a solution. I'm trying to build a mini-game and I have a method for creating platforms. I have a class with every platform parameters, and I made a class array so i can have multiple platforms at the same time.

Problem: When I try to call the method for constructing the platform by sending the parameters I want, it gives me a NullPointerException. The method was working before, but with everything static and so i couldnt have multiple instances of that class, and now I removed the static fields from the platform class and it gives me the NullPointerException every time I call the method.

I copied the part of the code that gives me the error, the error goes the following way:

public static void main(String[] args) {
        Game ex = new Game();
        new Thread(ex).start();
    }

In Game class:

public Load_Stage load = new Load_Stage();
public Game() {
        -other variables initializatin-
        Initialize_Items();
        load.Stage_1(); // <--- problem this way

In Load_Stage class:

public class Load_Stage {
    public Platforms plat = new Platforms();

    public void Stage_1(){    
        Stage_Builder.Build_Platform(200, 500, 300, plat.platform1);
        Stage_Builder.Build_Platform(100, 200, 100, plat.platform1);
    }

}

And inside the Stage_Builder class:

public class Stage_Builder {

    public static final int max_platforms = 10;
    public static Platform_1[] p1 = new Platform_1[max_platforms];
    public static boolean[] platform_on = new boolean[max_platforms];    

    public Stage_Builder() {
        for (int c = 0; c < platform_on.length; c++) {
            platform_on[c] = false;
        }
    }
    public static void Build_Platform(int x, int y, int width, ImageIcon[] type) { // BUILDS A PLATFORM

        for (int b = 0; b < max_platforms; b++) {
            if (platform_on[b] == false) { 
                p1[b].Construct(x, y, width, type); // <-- NullPointerException here
                platform_on[b] = true; 
                break;
            }
        }
    }
}

Thanks beforehand.

EDIT: Here's the Platform_1 class (sorry for forgetting about it):

public class Platform_1 {

    private int platform_begin_width = 30;
    private int platform_middle_width = 20;
    public int blocks_number = 0;
    public ImageIcon[] platform_floors = new ImageIcon[500];
    private int current_width = 0;
    public int [] platform_x = new int [500];
    public int platform_y = 0;
    public int platform_width = 0;

    public void Construct(int x, int y, int width, ImageIcon [] type) {        
        platform_width = width;
        platform_y = y;
        for (int c = 0; current_width <= platform_width; c++) { 
            if (c == 0) {
                platform_x[c] = x;
                platform_floors[c] = type[0];
                current_width += platform_begin_width;
            } else if ((current_width + platform_middle_width) > platform_width) {
                platform_floors[c] = type[2];
                blocks_number = c + 1;
                platform_x[c] = current_width + x;
                current_width += platform_middle_width;
            } else {
                platform_floors[c] = type[1];
                platform_x[c] = current_width + x;
                current_width += platform_middle_width;
            }
        }        
    }
}

And the Platforms class:

public class Platforms {

    public ImageIcon[] platform1 = {new ImageIcon("Resources/Sprites/Stage_Objects/Platform1/begin.png"),
        new ImageIcon("Resources/Sprites/Stage_Objects/Platform1/middle.png"),
        new ImageIcon("Resources/Sprites/Stage_Objects/Platform1/end.png")};
}

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

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

发布评论

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

评论(4

葮薆情 2025-01-05 12:28:06

问题和解决方案都是显而易见的。

public static Platform_1[] p1 = new Platform_1[max_platforms];

这行代码执行后,p1 是 Platform_1 类型的引用数组,全部为 null

执行这行代码立即告诉您:

            p1[b].Construct(x, y, width, type); // <-- NullPointerException here

解决方案是初始化 p1 数组以指向 Platform_1 的非空实例。

像这样的事情会起作用:

for (int i = 0; < p1.length; ++i) {
    p1[i] = new Platform1();
}

The problem and solution are both obvious.

public static Platform_1[] p1 = new Platform_1[max_platforms];

After this line of code executes, p1 is an array of references of type Platform_1 that are all null.

Executing this line of code tells you so right away:

            p1[b].Construct(x, y, width, type); // <-- NullPointerException here

The solution is to intialize the p1 array to point to non-null instances of Platform_1.

Something like this would work:

for (int i = 0; < p1.length; ++i) {
    p1[i] = new Platform1();
}
我们只是彼此的过ke 2025-01-05 12:28:06

我没有看到您将内容放在 Stage_Builder 类的 p1 数组中的位置。

另一种可能性(不太可能,但如果您没有显示所有内容,则有可能)是 Platform 类中您未显示的某些内容未初始化,并且在您调用 时中断构造

另外,以下内容似乎有问题

public static Platform_1[] p1 = new Platform_1[max_platforms];
public static boolean[] platform_on = new boolean[max_platforms];    

public Stage_Builder() {
   for (int c = 0; c < platform_on.length; c++) {
       platform_on[c] = false;
   }
}

,您声明静态变量 p1platform_on,但仅在构造函数中填充 platform_on 。因此,第一次创建 Stage_Builder 实例时,您将使用所有 false 填充一个静态数组,并且不要在另一个静态数组中放入任何内容...

将这些静态变量填充到静态块中

// static var declarations

static {
   // populate static arrays here.
}

I'm not seeing where you put things in the p1 array in the Stage_Builder class.

Another possibility (unlikely, but possible if you haven't shown everything) is that something in the Platform class, which you didn't show, is not initialized, and is breaking when you call Construct.

Also, the following seems problematic

public static Platform_1[] p1 = new Platform_1[max_platforms];
public static boolean[] platform_on = new boolean[max_platforms];    

public Stage_Builder() {
   for (int c = 0; c < platform_on.length; c++) {
       platform_on[c] = false;
   }
}

it appears you declare static variables p1 and platform_on, but you only populate platform_on in a constructor. So the first time you create a Stage_Builder instance, you populate one static array with all false, and don't put anything in the other static array...

Populate those static variables in a static block

// static var declarations

static {
   // populate static arrays here.
}
没有伤那来痛 2025-01-05 12:28:06

您调用消息的数组从未被填充。

你有

public static Platform_1[] p1 = new Platform_1[max_platforms];

这样的 p1

p1[0] = null
p1[1] = null
 .
 .
 .
p1[max_platforms] = null

你尝试调用

p1[b].Construct(x, y, width, type);

它是

null.Construct(...);

你需要首先初始化数组上的索引。

p1[b] = new Platform_1();
p1[b].Construct(...);

The array you are calling a message on was never filled.

You have

public static Platform_1[] p1 = new Platform_1[max_platforms];

so p1 is

p1[0] = null
p1[1] = null
 .
 .
 .
p1[max_platforms] = null

You try to call

p1[b].Construct(x, y, width, type);

which is

null.Construct(...);

You need to initialize that index on the array first.

p1[b] = new Platform_1();
p1[b].Construct(...);
甜味拾荒者 2025-01-05 12:28:06

首先,正如达菲莫指出的那样,您的问题是 p1[b] 很可能为空。

其次,您以一种非常奇怪的方式使用数组。怎么样

a) 删除 Stage_Builder

b) 相反,在某处有一个 ArrayList

c) 相当于 Build_Platform1() 看起来像这样,然后:

p1.add(new Platform1(x, y, width, type);

d) 没有 if on[i],没有 max_platforms,没有 for 循环来添加平台(后者如果您实际上有数百个平台,这是一个糟糕的性能问题)

First of all, your problem is that p1[b] is most likely null, as duffymo pointed out.

Secondly, you are using arrays in a really weird way. What about

a) delete Stage_Builder

b) Instead, have an ArrayList somewhere

c) The equivalent of Build_Platform1() look like this, then:

p1.add(new Platform1(x, y, width, type);

d) no if on[i], no max_platforms, no for loop to add a platform (the latter is a bad performance problem if you actually have a few hundret platforms)

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