• Advertisement
Sign in to follow this  

Newbie Nullpointer error in java

This topic is 1332 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement


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

Upvoted for you :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement