简体   繁体   中英

Calling a method from a class array gives NullPointerException

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

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

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM