Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to make a 2D tile game and when making the arrays holding the tiles I get a NullPointerException, here is some of the code. Sorry if this is poorly formatted, first timer

public class World {

//holds data for where to place images and rectangles
int[][] worldDat = new int[25][25]; 
//hold rectangles for checking interaction with player
Rectangle[][] blocks = new Rectangle[25][25];
//holds block's images to be painted
Image[][] blockImage = new Image[25][25];
//holds position to be pained on screen
int[][] location = new int[25][25];
//enumeration holding block's images and other things of the sort
EWorldBlocks eBlocks;

//sets all of the arrays listed above
public void setupAll(){

    for(int i = 0; i < 24; i++){

        for(int e = 0; e < 24; e++){                    
            blocks[i][e].setBounds(e * 20, i * 20, 20, 20);
            blocks[i][e].setLocation(e*20, i*20);

            if(worldDat[i][e] == 6){
                blockImage[i][e] = getRandomGrass();
            }else if(worldDat[i][e] == 0){
                blockImage[i][e] = null;
            }else{
            blockImage[i][e] = eBlocks.intToImage(worldDat[i][e]);
            }
        }
    }
}   

//used to get a random block
private Image getRandomGrass()
{
    Random rand = new Random();

    int r = rand.nextInt(2);
    r++;

    return eBlocks.intToImage(r);
}


public World(int[][] worldDat) {
    this.worldDat = worldDat;
}

}

Then that is called in this class (i believe its part of the problem)

public class worldDraw{

//ALSO if there is a better way to do this, do tell
levels levels = new levels();
static levels sLevels = new levels();
World level1;
static World sLevel1 = new World(sLevels.getLevel1());

//called in paint method for panel
public void draw(Graphics2D g2){
    sLevel1.setupAll();
    for(int i = 0; i < 24; i++){
        for(int e = 0; i < 24; i++){
            g2.drawImage(level1.blockImage[i][e], e*25, i*25, null);
        }
    }

}

//holds levels
public worldDraw() {        
    level1 = new World(levels.getLevel1());
}
}
share|improve this question

2 Answers

up vote 3 down vote accepted

When you create an object array, you are creating an array of references, but you are not assigning the references. You must do this first before trying to use them. Think of it as being similar to creating an egg carton. You can't use any eggs until you first fill the carton with eggs. So for instance your blocks array, you first need to assign Rectangle objects to each item in the array before you can call methods on them. This is usually done in a for loop. e.g.,

for(int i = 0; i < 24; i++){
    for(int e = 0; e < 24; e++){       
        blocks[i][e] = new Rectangle(....); //...             
        blocks[i][e].setBounds(e * 20, i * 20, 20, 20);
        blocks[i][e].setLocation(e*20, i*20);
share|improve this answer
How do I go about doing that? – Jack G Jan 29 at 3:15
1  
@Jack: please see edit. – Hovercraft Full Of Eels Jan 29 at 3:16
Thanks for the help! – Jack G Jan 29 at 3:18
@JackG: you're welcome! – Hovercraft Full Of Eels Jan 29 at 3:20

You need to know that Java is not like C.

When you do this:

Rectangle[][] blocks = new Rectangle[25][25];

All the references in the blocks 2D array are null until you call new and give them a reference.

So you'll have to do this:

for(int i = 0; i < 24; i++){
    for(int e = 0; e < 24; e++){             
        blocks[i][e] = new Rectangle(); // I don't know what arguments it takes.       
        blocks[i][e].setBounds(e * 20, i * 20, 20, 20);
        blocks[i][e].setLocation(e*20, i*20);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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