Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Newbie Nullpointer error in java

Started by DaysofReckoning Jul 1, 2014 at 12:37 AM 2 replies 1.7k views
Original Post
DaysofReckoning
DaysofReckoning

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

DiegoSLTS
DiegoSLTS

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.

Datamancer
Datamancer
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 ob
Lactose
Lactose




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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.