-5

My code is:

public void move() {
    moveX();
    moveY();
}

public void moveX() {
    if(xMove > 0) {
        int tx = (int) (x + xMove + bounds.x + bounds.width) / Tile.TILEWIDTH;

        if(!collisionWithTile(tx, (int) (y + bounds.y) / Tile.TILEHEIGHT) && !collisionWithTile(tx, (int) (y + bounds.y + bounds.height) / Tile.TILEHEIGHT)) {
            x += xMove;
        }
    } else if(xMove < 0) {
        int tx = (int) (x + xMove + bounds.x) / Tile.TILEWIDTH;

        if(!collisionWithTile(tx, (int) (y + bounds.y) / Tile.TILEHEIGHT) && !collisionWithTile(tx, (int) (y + bounds.y + bounds.height) / Tile.TILEHEIGHT)) {
            x += xMove;
        }


    }

}

public void moveY() {
    if(yMove < 0) {
        int ty = (int) (y + yMove + bounds.y + bounds.height) / Tile.TILEHEIGHT;

        if(!collisionWithTile((int) (x + bounds.x) / Tile.TILEHEIGHT, ty) && !collisionWithTile((int) (x + bounds.x + bounds.width) / Tile.TILEWIDTH, ty)) {
            y += yMove;
        }
    }

    if(yMove > 0) {
        int ty = (int) (y + yMove + bounds.y) / Tile.TILEHEIGHT;

        if(!collisionWithTile((int) (x + bounds.x) / Tile.TILEHEIGHT, ty) && !collisionWithTile((int) (x + bounds.x + bounds.width) / Tile.TILEWIDTH, ty)) {
            y += yMove;
        }
    }
}

protected boolean collisionWithTile(int x, int y) {
    return room.getTile(x, y).isSolid();
}

but when I run it I get a NullPointerException with this StackTrace: Exception in thread "Thread-0" java.lang.NullPointerException at com.pixelandbitgames.entities.creature.Creature.collisionWithTile(Creature.java:69) at com.pixelandbitgames.entities.creature.Creature.moveX(Creature.java:41) at com.pixelandbitgames.entities.creature.Creature.move(Creature.java:27) at com.pixelandbitgames.entities.creature.Player.tick(Player.java:19) at com.pixelandbitgames.states.GameState.tick(GameState.java:23) at com.pixelandbitgames.game.Game.tick(Game.java:72) at com.pixelandbitgames.game.Game.run(Game.java:113) at java.lang.Thread.run(Thread.java:748) I do not see where this is coming from as collisionWithTile has all its parameters filled. If anyone can help me that would be appriciated. if anyone needs any thing else to help I will deliver. My other classes seem fine and it is just this one. This happens when I try to move. my code for Room.getTile is here:

public Tile getTile(int x, int y) {
    if(x < 0 || y < 0 || x >= width || y >= height)
        return Tile.floorTile;
    Tile t = Tile.tiles[tiles[x][y]];
    if(t == null)
        return Tile.floorTile;
    return t;
}

and this has no way to return null. Is solid is just a return true unless it is overridden.

public boolean isSolid() {
    return false;
}
row666
  • 47
  • 5

1 Answers1

0

Either room is null, or room.getTile(x, y) returns null. You haven't pasted the relevant bits of those, making it impossible to give more information.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72