从类数组调用方法会给出 NullPointerException
我一直在寻找这个问题很多,但找不到解决方案。我正在尝试构建一个迷你游戏,并且我有一种创建平台的方法。我有一个包含每个平台参数的类,并且我创建了一个类数组,这样我就可以同时拥有多个平台。
问题:当我尝试通过发送我想要的参数来调用构建平台的方法时,它给了我一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题和解决方案都是显而易见的。
这行代码执行后,p1 是 Platform_1 类型的引用数组,全部为 null。
执行这行代码立即告诉您:
解决方案是初始化
p1
数组以指向Platform_1
的非空实例。像这样的事情会起作用:
The problem and solution are both obvious.
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:
The solution is to intialize the
p1
array to point to non-null instances ofPlatform_1
.Something like this would work:
我没有看到您将内容放在
Stage_Builder
类的p1
数组中的位置。另一种可能性(不太可能,但如果您没有显示所有内容,则有可能)是
Platform
类中您未显示的某些内容未初始化,并且在您调用时中断构造
。另外,以下内容似乎有问题
,您声明静态变量
p1
和platform_on
,但仅在构造函数中填充platform_on
。因此,第一次创建 Stage_Builder 实例时,您将使用所有false
填充一个静态数组,并且不要在另一个静态数组中放入任何内容...将这些静态变量填充到静态块中
I'm not seeing where you put things in the
p1
array in theStage_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 callConstruct
.Also, the following seems problematic
it appears you declare static variables
p1
andplatform_on
, but you only populateplatform_on
in a constructor. So the first time you create a Stage_Builder instance, you populate one static array with allfalse
, and don't put anything in the other static array...Populate those static variables in a static block
您调用消息的数组从未被填充。
你有
这样的
p1
是你尝试调用
它是
你需要首先初始化数组上的索引。
The array you are calling a message on was never filled.
You have
so
p1
isYou try to call
which is
You need to initialize that index on the array first.
首先,正如达菲莫指出的那样,您的问题是 p1[b] 很可能为空。
其次,您以一种非常奇怪的方式使用数组。怎么样
a) 删除 Stage_Builder
b) 相反,在某处有一个 ArrayList
c) 相当于 Build_Platform1() 看起来像这样,然后:
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:
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)