문제

I'm making an rpg with a custom pixel engine, and when I try to run it I just get a NullPointerException and I know why, but when I try to initialize that variable I get another one in a different location and I fix that and now it won't run at all and I still get a NullPointerException. This is the class that gets an error, and the console tells me that the error is on the line that says screen.render();

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import sprites.SpriteSheetLoader;

public class Game extends Canvas implements Runnable{

    private static final long serialVersionUID = 1L;

    public static final int HEIGHT = 120;
    public static final int WIDTH = 120;
    public static final int SCALE = 3;
    public static final String NAME = "TypicalRPG";
    public SpriteSheetLoader loader;

    private BufferedImage img = new BufferedImage(WIDTH * SCALE, HEIGHT * SCALE, BufferedImage.TYPE_INT_RGB);
    private int[] pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
    private boolean running = false;
    private Screen screen = new Screen(WIDTH, HEIGHT, loader);

    Random random = new Random();

    public void start(){

        running = true;

        new Thread(this).start();

    }

    public static void main(String args[]){

        Game game = new Game();

        game.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
        game.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
        game.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));

        JFrame jf = new JFrame(NAME);

        jf.add(game);

        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jf.pack();

        jf.setVisible(true);
        jf.setResizable(true);

        game.start();

    }

    public void run(){

        while(running){

            tick();

            render();

            try{

                Thread.sleep(5);

            }

            catch(InterruptedException e){ e.printStackTrace(); }

        }

    }

    public void stop(){ running = false; }

    public void tick(){

        screen.render(0, 0, 0, 16, 16);

    }

    public void render(){

        BufferStrategy bs = getBufferStrategy();

        if(bs == null){

            createBufferStrategy(3);

            requestFocus();

            return;

        }

        for(int i = 0; i < screen.pixels.length; i++){

            pixels[i] = screen.pixels[i];

        }

        Graphics g = bs.getDrawGraphics();

        g.drawImage(img, 0, 0, getWidth(), getHeight(), null);

        g.dispose();

        bs.show();

    }

    public void init(){

        BufferedImage sheet = null;

        try {

            sheet = ImageIO.read(Game.class.getResourceAsStream("res/tiles.png"));

        }

        catch (IOException e) {

            e.printStackTrace();

    }

    loader = new SpriteSheetLoader(sheet);

    screen = new Screen(WIDTH, HEIGHT, loader);

    }

}

and this is the class that has render():

import sprites.SpriteSheetLoader;

public class Screen{

    public int[] pixels;

    private SpriteSheetLoader loader;
    private int w, h;

    int xoff = 0, yoff = 0;

    public Screen(int w, int h, SpriteSheetLoader loader){

        this.loader = loader;
        this.w = w;
        this.h = h;

        pixels = new int[w * h];

    }

    public void render(int xpos, int ypos, int tile, int width, int height){

        loader.grabTile(tile, width, height);

        xpos -= xoff;
        ypos -= yoff;

        for(int y = 0; y < height; y++){

            if(ypos + y < 0 || ypos + y >= h) continue;

            for(int x = 0; x < width; x++){

                if(xpos + x < 0 || xpos + x >= w) continue;

                int col = loader.pixels[x + (y * height)];

                if(col != -65281) pixels[(x + xpos) + (y + ypos)] = col;

            }

        }

    }

}
도움이 되었습니까?

해결책

Seems like you never initiate the loader in your first class.

You define your variables like this:

public SpriteSheetLoader loader;
private Screen screen = new Screen(WIDTH, HEIGHT, loader);

Hence, you pass a null-object to your Screen. And from there you try to call a method on that null-object:

loader.grabTile(tile, width, height);

다른 팁

You have only declared loader, but never initiated it.

public SpriteSheetLoader loader;

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top