-5

I am having a problem where I create an array of objects but I get a java.lang.NullPointerException when i try to address something to it.

this is the class with the problem.

public class Blocks {
public static Block[] b = new Block[8];

public Blocks() throws IOException {
    new Air  (b[0]);
    new Stone(b[1]);
    new Grass(b[2]);
    new Dirt (b[3]);
}

this is the class Block.

public class Block {
private Texture Texture = null;
private int S = World.BLOCK_SIZE;
private boolean hasTexture = true;
private String texturePath = null;

public void setTexture(String path) throws IOException {
    this.texturePath = path;
    Texture = TextureLoader.getTexture("PNG", new FileInputStream(new File(path)));
}

public void draw(int Xa, int Ya) {

    GL11.glTranslatef(Xa, Ya, 0);
    //GL11.glRotatef(0, 0, 1, 0);
    //GL11.glRotatef(0, 1, 0, 0);

    if(hasTexture) {
    Texture.bind();

    GL11.glBegin(GL11.GL_QUADS);
        GL11.glColor3f(0.5f, 0.5f, 1);
        //GL11.glNormal3f(0, 0, 1);
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex2f(0, 0);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex2f(0, S);
        GL11.glTexCoord2f(1, 1);
        GL11.glVertex2f(S, S);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex2f(S, 0);

        GL11.glEnd();
    }
}

void hasTexture(boolean b) {
    this.hasTexture = b;
}

}

please let me know if i need to provide more info/code

6
  • You should provide error information (error message, line, stacktrace) Commented Feb 27, 2013 at 5:51
  • I think there is a problem with getting the texture: Texture = TextureLoader.getTexture("PNG", new FileInputStream(new File(path))); Commented Feb 27, 2013 at 5:52
  • texture is null and when you are trying to work with null object you get NullPointerException Commented Feb 27, 2013 at 5:52
  • Use static initializer block for static fields, or static lazy getter. And assign objects to your array properly: b[0] = new Block() Commented Feb 27, 2013 at 5:56
  • 1
    Downvoting this into oblivion isn't really helpful. Yes it could be improved, but no need to go ballistic on this one. Commented Feb 27, 2013 at 6:02

3 Answers 3

3

Make it this way

public class Blocks {
public static Block[] b = new Block[8];

static {
   // Instantiating the objects present in the array
   for(int i=0; i<b.length; i++)
       b[i] = new Block();
}

public Blocks() throws IOException {
   // Now you can access them
    new Air  (b[0]);
    new Stone(b[1]);
    new Grass(b[2]);
    new Dirt (b[3]);
}

You forgot to instantiate the objects present in the array. So it is prompting null pointer exception

3
  • It is not a good idea to initialize static field within a constructor. Static initializer block or static (lazy) getter would be a better idea. Commented Feb 27, 2013 at 6:07
  • @skuntsel Yeah right! Otherwise whenever a new object is created the array would have set to initial value. Thanks for correcting me. I have updated my answer with it. Thanks! :) Commented Feb 27, 2013 at 6:09
  • Thanks. this fixed it. so i have to init the obects also? Commented Feb 27, 2013 at 6:21
2

It would appear that you are creating an empty array named b with 8 slots (of type block), and then instantiating objects (such as new Air (b[0]);) using references to the (empty) array.

If the constructor for Air can't handle a null argument that may be the source of your null pointer exception

Try putting some objects in b first

2

For starters, you can look into this:-

public static Block[] b = new Block[8];

public Blocks() throws IOException {
    new Air  (b[0]);
    new Stone(b[1]);
    new Grass(b[2]);
    new Dirt (b[3]);
}

You've not instantiated the array elements. b[0], b[1], etc.. are still null references.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.