2012-01-26 34 views
0

我一直在为这个问题寻找很多,我找不到解决方案。我试图建立一个迷你游戏,我有一个创建平台的方法。我有一个每个平台参数的类,我做了一个类的数组,所以我可以在同一时间有多个平台。从类数组中调用方法会导致NullPointerException

问题:当我尝试通过发送参数来调用构建平台的方法时,它给了我一个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")}; 
} 

回答

3

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

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

这行代码执行后,p1为Platform_1 是所有空类型的引用数组。

执行这行代码告诉你,马上:

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

的解决方案是并初始化的p1阵列指向的Platform_1非空的情况。

像这样的工作:

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

对不起忘记p1数组代码。我编辑了这篇文章。我会检查你的解决方案,看看它是否工作,谢谢:) –

+0

好吧,我如何初始化P1,指向Platform_1的非null实例?我知道你的意思,我只是不知道如何实施解决方案 –

+0

查看示例代码 – duffymo

2

我没有看到你把东西p1阵列在Stage_Builder类。

另一种可能性(不太可能,但如果您未显示所有内容,则可能)是Platform类中未显示的内容未初始化,当您拨打Construct时该内容已中断。

此外,下面的问题似乎

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. 
} 
+0

对不起,忘记了p1数组代码。我编辑了这篇文章。我会检查你的解决方案,看看它是否工作,谢谢:) –

+0

你在哪里填充'p1'数组? – hvgotcodes

+0

我实际上......没有填充它,我现在就做,看看是否有效。我认为这不是问题,因为我做了像以前一样的东西,我用相同的逻辑做了它......但我想我会使用一个ArrayList作为Kosta建议,看看是否有用。谢谢。 –

0

数组你正在呼吁从未填充的消息。

你有

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(...); 
+0

谢谢,这似乎是问题所在。我现在正在研究它:D –

0

首先,您的问题是,正如duffymo指出的,p1 [b]很可能为空。其次,你正在以一种奇怪的方式使用数组。什么

一)删除Stage_Builder

B)相反,有一个ArrayList地方

C)Build_Platform1的等效()这个样子,那么:

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

d)无如果在[i]上没有max_platforms,则不需要for循环来添加平台(如果您实际上有几个hundret平台,后者是一个糟糕的性能问题)

+0

哇,好像是一个很好的改进。谢谢,我其实并没有处理过ArrayLists,但生病了你的建议。至于这个问题,我使用p1(Platform_1)类代码编辑了帖子。据我所知,一切都在那里正确初始化。感谢:D –

相关问题