Newbie Nullpointer error in java

Started by
2 comments, last by Lactose 9 years, 9 months ago

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

at realm.level.Level.render(Level.java:49)
at realm.Realm.render(Realm.java:112)
at realm.Realm.run(Realm.java:81)
at java.lang.Thread.run(Thread.java:745)

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);
    }
}

Advertisement

Tile class has an uninitialized static variable called grass, when you return Tile.grass you're returning null. I guess you must change it to "return GrassTile.grass;", or initialize the "grass" static variable on Tile class too.

Sorry Diego, I meant to up vote you, but I accidently hit down vote on my phone.

"The code you write when you learn a new language is shit.
You either already know that and you are wise, or you don’t realize it for many years and you are an idiot. Either way, your learning code is objectively shit." - L. Spiro

"This is called programming. The art of typing shit into an editor/IDE is not programming, it's basically data entry. The part that makes a programmer a programmer is their problem solving skills." - Serapth

"The 'friend' relationship in c++ is the tightest coupling you can give two objects. Friends can reach out and touch your privates." - frob


Sorry Diego, I meant to up vote you, but I accidently hit down vote on my phone.

Upvoted for you :)

Hello to all my stalkers.

This topic is closed to new replies.

Advertisement