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.

I can not provide error as I have fixed.

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

}

I have succeeded with this before.

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

share|improve this question
You should provide error information (error message, line, stacktrace) – default locale yesterday
I think there is a problem with getting the texture: Texture = TextureLoader.getTexture("PNG", new FileInputStream(new File(path))); – Rafael Osipov yesterday
texture is null and when you are trying to work with null object you get NullPointerException – Rafael Osipov yesterday
Use static initializer block for static fields, or static lazy getter. And assign objects to your array properly: b[0] = new Block() – skuntsel yesterday
1  
Downvoting this into oblivion isn't really helpful. Yes it could be improved, but no need to go ballistic on this one. – Gus yesterday
show 1 more comment

closed as too localized by John3136, TheWhiteRabbit, Sankar Ganesh, Rikesh, luser droog yesterday

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

3 Answers

up vote 4 down vote accepted

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

share|improve this answer
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. – skuntsel yesterday
@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! :) – asifsid88 yesterday
Thanks. this fixed it. so i have to init the obects also? – FuZi0nHD yesterday
You're welcome :) – asifsid88 yesterday

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

share|improve this answer

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.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.