gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:33:12 25 4
gpt4 key购买 nike

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

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

我复制了导致错误的代码部分,错误如下:

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

在游戏类中:

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

以及平台类:

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")};
}

最佳答案

问题和解决方案都很明显。

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();
}

关于java - 从类数组调用方法会给出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9020232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com