Well, I’m making a relatively simple platformer game with java and I have a method that generates the level, but the tiles won’t draw.
public void generateDungeon() {
int y = 30;
for(int x = 0; x < block[0].length; x++) {
block[x][y] = new Block(new Rectangle(x * Tile.tileSize, y * Tile.tileSize, Tile.tileSize, Tile.tileSize), Tile.basic);
}
}
I want the Y to stay the same so that’s why it has a set value.
That is where I think the problem lies, but here’s the console message
Exception in thread "Thread-2" java.lang.NullPointerException
at mineandbuild.Player.isCollidingWithBlock(Player.java:82)
at mineandbuild.Player.tick(Player.java:22)
at mineandbuild.Component.tick(Component.java:89)
at mineandbuild.Component.run(Component.java:110)
at java.lang.Thread.run(Unknown Source)
The console says the problem is at this line of code:
public boolean isCollidingWithBlock(Point pt1, Point pt2) {
for(int x = (int) (this.x / Tile.tileSize); x < (int) (this.x / Tile.tileSize + 3); x++) {
for(int y = (int) (this.y / Tile.tileSize); y < (int) (this.y / Tile.tileSize + 3); y++) {
if(x >= 0 && y >= 0 && x < Component.dungeon.block.length && y < Component.dungeon.block[0].length)
This line ---> if(Component.dungeon.block[x][y].id != Tile.air) {
if(Component.dungeon.block[x][y].contains(pt1) || Component.dungeon.block[x][y].contains(pt2)) {
return true;
}
}
}
}
return false;
}
Thanks!
Try printing out the x and y values for
You say that in the code before the value of y should stay the same, which means for every other index of y in the 2-d array they’re set to null, but in the forloop you have this
which leads me to believe that you’re trying to access indices that are null, then trying to access the “id” field, except that that object doesn’t exist.