0

package net.gfx;

public class TileSet {

    public final int TILES = 627;

    class Tiles {
        int x = 0, y = 0;
        int w = 0, h = 0;

    }

    public Tiles tiles[] = new Tiles[TILES];

    public TileSet() {
        for (int i = 0, y = 0; i < TILES; i++) {
            for (int x = 0; x < 1280; x =+ 25) {
                if (x > 1280) {
                    x = 0;
                    y += 40;

                }
                else {
                    tiles[i].x = x; //ERROR CAUSED HERE
                    tiles[i].y = y; //TO HERE *Unknown reason*
                    tiles[i].w = 40;
                    tiles[i].h = 40;

                }

            }

        }

    }

}

The Error im getting:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at net.jump.Jump.<clinit>(Jump.java:8)
Caused by: java.lang.NullPointerException
    at net.gfx.TileSet.<init>(TileSet.java:24)
    at net.gfx.Graphics.<clinit>(Graphics.java:10)
    ... 1 more

What I'm trying to do is basically create an array of tiles on a screen. Everyting else works besides the setting the Object array Values.

I've searched almost everywhere and haven't found anything. I bet its probaly some simple thing i missed.

1
  • Can you provide more Infos about the Framework you use for the Tiles? Simple approach without further knowledge: Why don't you set the values before you store the tiles in the set? Commented Sep 5, 2013 at 0:57

2 Answers 2

2

You have to create an instance of Tiles before you can do any operation on it.

 for (int i = 0, y = 0; i < TILES; i++) {
    for (int x = 0; x < 1280; x =+ 25) {
        if (x > 1280) {
            x = 0;
            y += 40;

        }
        else {
            tiles[i] = new Tiles(); //instance created here.
            tiles[i].x = x; 
            tiles[i].y = y; 
            tiles[i].w = 40;
            tiles[i].h = 40;

        }

    }

}
Sign up to request clarification or add additional context in comments.

Comments

1

You aren't initializing each Tiles in your array, as the default value is null.

Perhaps you should try initializing each Tiles:

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

After that, you could perform operations with each Tiles element in the array.

1 Comment

I've Implemented that code and every time I run it just freezes up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.