出于某种原因获取NullPointerException

时间:2015-03-07 13:37:58

标签: java

我正在关注来自ChernoProject的教程,但是在第22集中,我一直在尝试从精灵渲染像素数组时得到nullPointerException?

错误:

Exception in thread "Game" java.lang.ExceptionInInitializerError
    at com.santec.game.learning.thecherno.graphics.Screen.render(Screen.java:40)
    at com.santec.game.learning.thecherno.Game.render(Game.java:109)
    at com.santec.game.learning.thecherno.Game.run(Game.java:78)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at com.santec.game.learning.thecherno.graphics.Sprite.load(Sprite.java:23)
    at com.santec.game.learning.thecherno.graphics.Sprite.<init>(Sprite.java:17)
    at com.santec.game.learning.thecherno.graphics.Sprite.<clinit>(Sprite.java:10)
    ... 4 more

课程:

Game.java:

package com.santec.game.learning.thecherno;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;

import com.santec.game.learning.thecherno.graphics.Screen;
import com.santec.game.learning.thecherno.input.Keyboard;

public class Game extends Canvas implements Runnable {
        private static final long serialVersionUID = 1L;

        public static int width = 300;
        public static int height = width / 16 * 9;
        public static int scale = 3;
        public static String title = "Rain";

        private Thread thread;
        private JFrame frame;
        private boolean running = false;

        private Screen screen;
        private Keyboard key;

        private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();

        public Game() {
                Dimension size = new Dimension(width * scale, height * scale);
                setPreferredSize(size);

                screen = new Screen(width, height);
                frame = new JFrame();
                key = new Keyboard();

                addKeyListener(key);
        }

        public synchronized void start() {
                running = true;
                thread = new Thread(this, "Game");
                thread.start();
        }

        public synchronized void stop() {
                running = false;
                try {
                        thread.join();
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
        }

        public void run() {
                long lastTime = System.nanoTime();
                long timer = System.currentTimeMillis();
                final double ns = 1000000000.0 / 60.0;
                double delta = 0;
                int frames = 0;
                int updates = 0;
                while(running) {
                        long now = System.nanoTime();
                        delta += (now - lastTime) / ns;
                        lastTime = now;
                        while(delta >= 1) {
                                update();
                                updates++;
                                delta--;
                        }
                        render();
                        frames++;

                        if(System.currentTimeMillis() - timer > 1000) {
                                timer += 1000;
                                frame.setTitle(title + ": " + updates + " ups | " + frames + " fps");
                                updates = 0;
                                frames = 0;
                        }
                }
                stop();
        }

        int x = 0, y = 0;

        public void update() {
                key.update();
                if(key.up) y--;
                if(key.down) y++;
                if(key.right) x++;
                if(key.left) x--;
        }

        public void render() {
                BufferStrategy bs = getBufferStrategy();
                if(bs == null) {
                        createBufferStrategy(3);
                        return;
                }

                screen.clear();
                screen.render(x, y);

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

                Graphics g = bs.getDrawGraphics();

                {
                        g.setColor(Color.cyan);
                        g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
                }

                g.dispose();
                bs.show();
        }

        public static void main(String[] args) {
                Game game = new Game();
                game.frame.setResizable(false);
                game.frame.setTitle(game.title);
                game.frame.add(game);
                game.frame.pack();
                game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                game.frame.setLocationRelativeTo(null);
                game.frame.setVisible(true);

                game.start();
        }

}

graphics / SpriteSheet.java:

package com.santec.game.learning.thecherno.graphics;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SpriteSheet {

    private String path;
    public final int SIZE;
    public int[] pixels;

    // Spritesheets
    public static SpriteSheet terrain = new SpriteSheet("/textures/terrain.png", 96);

    public SpriteSheet(String path, int size) {
        this.path = path;
        SIZE = size;
        pixels = new int[SIZE * SIZE];
        load();
    }

    private void load() {
        try {
            BufferedImage image = ImageIO.read(SpriteSheet.class.getResource(path));
            int w = image.getWidth();
            int h = image.getHeight();
            image.getRGB(0, 0, w, h, pixels, 0, w);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

graphics / Screen.java:

package com.santec.game.learning.thecherno.graphics;

import java.util.Random;

public class Screen {
    private int width, height;
    public int[] pixels;
    public final int MAP_SIZE = 8;
    public final int MAP_SIZE_MASK = MAP_SIZE - 1;

    public int[] tiles = new int[MAP_SIZE * MAP_SIZE];

    private Random random = new Random();

    public Screen(int width, int height) {
        this.width = width;
        this.height = height;
        pixels = new int[width * height];

        for(int i = 0; i < MAP_SIZE * MAP_SIZE; i++) {
            tiles[i] = random.nextInt(0xffffff);
            tiles[0] = 0;
        }   
    }

    public void clear() {
        for(int i = 0; i < pixels.length; i++) {
            pixels[i] = 0;
        }
    }

    public void render(int xOffset, int yOffset) {
        for(int y = 0; y < height; y++) {
            int yy = y + yOffset;
            if(yy >= height || yy < 0) break;
            for(int x = 0; x < width; x++) {
                int xx = x + xOffset;
                if(xx >= width || xx < 0) break;
                int tileIndex = ((xx >> 4) & MAP_SIZE_MASK) + ((yy >> 4) & MAP_SIZE_MASK) * 8;
                pixels[x + y * width] = Sprite.grass.pixels[(xx & 15) + (yy & 15) * Sprite.grass.SIZE];
            }
        }
    }


}

graphics / Sprite.java:

package com.santec.game.learning.thecherno.graphics;

public class Sprite {

        public final int SIZE;
        private int x, y;
        public int[] pixels;
        private SpriteSheet sheet;

        public static Sprite grass = new Sprite(16, 0, 1, SpriteSheet.terrain);

        public Sprite(int size, int x, int y, SpriteSheet sheet) {
                SIZE = size;
                this.x = x * size;
                this.y = y * size;
                this.sheet = sheet;
                load();
        }

        private void load() {
                for(int y = 0; y < SIZE; y++) {
                        for(int x = 0; x < SIZE; x++) {
                                pixels[x + y * SIZE] = sheet.pixels[(x + this.x) + (y + this.y) * sheet.SIZE];
                        }
                }
        }


}

1 个答案:

答案 0 :(得分:1)

您永远不会初始化pixels数组。

添加

this.pixels = new int[SIZE*SIZE]; // assuming that's the desired length or that array

load方法的开头。