0

Sorry if I'm not explaining good for it's 12:34 am and i'm doing late night programming but I need help. Btw this is in LWJGL Here's my Code:

I keep getting a null pointer error for the addAt() and the draw(); Basicly there is a couple classes that make it so when I click it will run addAt(mousex,mousey); and in the render loop it will keep drawing. The class that is new Block(x,y) is a class that will draw the QUAD.

//beggining
public class Grid {

    Block[][] blocks = new Block[25][25];
    public Grid(){
        for (int x = 0; x < 25 - 1; x++) {
            for (int y = 0; y < 16 - 1; y++) {
                blocks[x][y] = new Block(x,y);
            }
        }
    }
    public void draw(){

        for (int x = 0; x < 25;x++){
            for (int y = 0; y < 25;y++){
                blocks[x][y].draw();
            }   
        }

    }
    public void addAt(int x,int y){

        blocks[x][y] = new Block(x,y);

    }
}
//end 

basicly the Main is just making a Display and running the draw loop and the input listener.

Then the Block class is just making a quad at the defined x and y.

Sorry if I disobeyed a stack overflow rule. This is my First post and it's at a late time.:) Thanks in advance!

2
  • Can you print the parameters passed to addAt() call. It will help in debugging further. Commented Apr 20, 2012 at 4:54
  • Are you sure you're getting an exception on the addAt call? It shouldn't make any problems unless blocks is null, and it can't be. The problem probably occurs on draw. Commented Apr 20, 2012 at 4:58

2 Answers 2

2

While adding to block array your looping is from 0 to (25-1) and (16-1). While processing the block the looping is from 0 to 25. That would most probably lead to NPE. Try initializing you blocks from 0 to 25 (for both x and y values).

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

1 Comment

More specifically, both the loops should read like for(int x=0; x<25; x++)
0

I tried your code and it works fine (I don't get any exception). Here is my code:

public class Test {

    private class Block {
        int x, y;

        private Block(int x, int y) {
            this.x = x;
            this.y = y;
        }


        void draw() {

        }
    }


    Block[][] blocks = new Block[25][25];

    public Test(){
        for (int x = 0; x < 25 - 1; x++) {
            for (int y = 0; y < 16 - 1; y++) {
                blocks[x][y] = new Block(x,y);
            }
        }
    }
    public void draw(){

        for (int x = 0; x < 25;x++){
            for (int y = 0; y < 25;y++){
                blocks[x][y].draw();
            }
        }

    }
    public void addAt(int x,int y){
        blocks[x][y] = new Block(x,y);
    }



    public static void main(String[] args) {
        Test t = new Test();
        t.addAt(4,5);
    }
}

=> The problem seems to be the late night programming ;-)

1 Comment

well the problem is in the game loop I run grid.draw so it updates properly. That is what's causing the null pointer and I have no clue how to fix that.

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.