Hey I'm new to coding I've only been doing it a few weeks. I've been following this tutorial on youtube but when I try to run the program im getting this error:
Exception in thread "Display" java.lang.NullPointerException
The error occurs at line 49 "getTile(x,y).render(x,y,screen);" it seems to only happen when x or y is zero. Also the code compiles with no error when line 56 of the same class returns voidTile instead of grass. Which leads me to believe that the error has to do with my declaration of grass but I cant figure out how to fix it.
package realm.level;
import realm.graphics.Screen;
import realm.level.tile.GrassTile;
import realm.level.tile.Tile;
public class Level {
protected int width, height;
protected int[] tiles;
public Level(int width, int height){
this.width = width;
this.height = height;
tiles = new int [width * height];
generateLevel();
}
public Level (String path){
loadLevel(path);
}
protected void generateLevel(){
}
public void loadLevel (String path){
}
public void update(){
}
private void time(){
}
public void render(int xScroll, int yScroll, Screen screen){
screen.setOffset(xScroll, yScroll);
int x0 = xScroll >> 4;
int x1 = (xScroll + screen.width) >> 4;
int y0 = yScroll >> 4;
int y1 = (yScroll + screen.height) >> 4;
for (int y = y0; y < y1; y++){
for (int x = x0; x < x1; x++){
getTile(x, y).render(x, y, screen);
}
}
}
public Tile getTile(int x, int y) {
if (x < 0 || y < 0 || x >= width || y >= height ) return Tile.voidTile;
if (tiles[x + y * width] == 0) return Tile.grass;
return Tile.voidTile;
}
}
package realm.level.tile;
import realm.graphics.Screen;
import realm.graphics.Sprite;
public class Tile {
public static Tile grass;
public static Tile voidTile = new VoidTile(Sprite.voidSprite);
public int x, y;
public Sprite sprite;
public Tile (Sprite sprite) {
this.sprite = sprite;
}
public void render(int x, int y, Screen screen) {
}
public boolean solid () {
return false;
}
}
package realm.level.tile;
import realm.graphics.Screen;
import realm.graphics.Sprite;
public class GrassTile extends Tile{
public static Tile grass = new GrassTile(Sprite.grass);
public GrassTile(Sprite sprite) {
super(sprite);
}
@Override
public void render(int x, int y, Screen screen) {
screen.renderTile(x << 4, y << 4, this);
}
}