java - 我的游戏中出现 NullPointerException,无法修复

标签 java nullpointerexception null-pointer

我正在制作一个带有自定义像素引擎的角色扮演游戏,当我尝试运行它时,我只得到一个 NullPointerException,我知道为什么,但是当我尝试初始化该变量时,我在不同的位置得到另一个,我修复它,现在它根本无法运行,我仍然得到 NullPointerException。这是出现错误的类,控制台告诉我错误出在显示 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);

    }

}

这是具有 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;

            }

        }

    }

}

最佳答案

似乎您从未在第一个类中启动加载程序。

您可以这样定义变量:

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

因此,您将一个空对象传递给您的屏幕。然后从那里尝试调用该空对象的方法:

loader.grabTile(tile, width, height);

关于java - 我的游戏中出现 NullPointerException,无法修复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11924051/

相关文章:

java - 每第三次反弹改变颜色

java - JXMapViewer 混搭示例

java - 使用带有属性映射的 Cypher CREATE 命令,来自 Java

c++ - 尝试通过共享指针使用变量时读取访问冲突

java - setContentPane() 和 addActionListener 收到 NullPointerException

c++ - 代码中未初始化的指针

c - 不使用 * operator hack-y 方式获取产品

java - 即使包含包,Maven 也找不到类

Java、对象、构造函数 : Getting a NullPointerException error

c++ - 空指针的奇怪行为