2012-08-12 61 views
1

我正在制作一个带有自定义像素引擎的RPG,当我尝试运行它时,我只是得到一个NullPointerException异常,我知道为什么,但是当我尝试初始化该变量时我在另一个位置得到另一个,我修复了这个问题,现在它不会运行,我仍然得到一个NullPointerException。这是得到错误的类,并且控制台告诉我错误在screen.render()的行上。NullPointerException在我的游戏中,无法修复

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

    } 

} 

,这是具有渲染(类):

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; 

      } 

     } 

    } 

} 
+1

你能后的错误的一个实际的堆栈跟踪? – casablanca 2012-08-12 17:06:05

回答

5

好像你从来没有开始你的第一个类加载器。

你定义的变量是这样的:

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

因此,你传递一个空对象您的画面。并从那里你尝试调用一个方法是空对象:

loader.grabTile(tile, width, height); 
+2

+1它似乎从不调用init()。 – casablanca 2012-08-12 17:07:54

1

只宣布loader,但从未启动它。

public SpriteSheetLoader loader;

+0

我知道,但是当我做这个游戏无法正常工作... – SuperCheezGi 2012-08-14 22:24:52