2013-10-04 101 views
-1

我正在制作一个程序,具体的游戏,所以我开始使用基本的东西,但是当我尝试测试它时,它会退出奇怪的错误,它不会出现在代码,所以我想我的代码是好的,但我不知道可能是什么原因造成的。 :(Java - Eclipse IDE,由指针异常终止程序终止程序

这是错误:

Exception in thread "main" java.lang.NullPointerException 
    at java.awt.image.BufferedImage.<init>(Unknown Source) 
    at ca.hawk.game.Game.<init>(Game.java:33) 
    at ca.hawk.game.Game.main(Game.java:126) 

而这里的代码(包):

package ca.hawk.game; 

import java.awt.BorderLayout; 
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 java.awt.image.IndexColorModel; 

import javax.swing.JFrame; 

public class Game extends Canvas implements Runnable{ 

    private static final long serialVersionUID = 1L; 

    public static final int WIDTH = 160; 
    public static final int HEIGHT = WIDTH/12 * 9; 
    public static final int SCALE = 3; 
    public static final String NAME = "Game"; 

    private static final int BufferedImage = 0; 

    private static final IndexColorModel TYPE_INT_RGB = null; 

    private JFrame frame; 

    public boolean running = false; 
    public int tickCount = 0; 

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

    public Game(){ 
     setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); 
     setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); 
     setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); 

     frame = new JFrame(NAME); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLayout(new BorderLayout()); 

     frame.add(this, BorderLayout.CENTER); 
     frame.pack(); 

     frame.setResizable(false); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

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

    private synchronized void stop(){ 
     running = false; 
    } 

    public void run() { 
     long lastTime = System.nanoTime(); 
     double nsPerTick = 1000000000D/60D; 

     int ticks = 0; 
     int frames = 0; 

     long lastTimer = System.currentTimeMillis(); 
     double delta = 0; 
     while (running){ 
      long now = System.nanoTime(); 
      delta += (now - lastTime)/nsPerTick; 
      lastTime = now; 
      boolean shouldRender = true; 

      while (delta >= 1){ 
       ticks++; 
       tick(); 
       delta -= 1; 
       shouldRender = true; 
      } 
      try { 
       Thread.sleep(2); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      if (shouldRender){ 
       frames++; 
       render(); 
      } 

      if (System.currentTimeMillis() - lastTimer > 1000){ 
       lastTimer += 1000; 
       System.out.println(frames +", "+ ticks); 
       frames = 0; 
       ticks = 0; 
      } 
     } 
    } 

    public void tick(){ 
     tickCount++; 
    } 

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

     Graphics g = bs.getDrawGraphics(); 

     g.setColor(Color.BLACK); 
     g.fillRect(0, 0, getWidth(), getHeight()); 

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

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

    public static void main(String[] args){ 
     new Game().start(); 
    } 
} 
+0

TYPE_INT_RGB是空 –

+0

BTW我使用本教程http://www.youtube.com/watch?v=VE7ezYCTPe4仍然IDK的他为什么0的错误,因为我完全地写相同的 – Martin

+0

你的代码是不一样的 –

回答

0

变化:

private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage, TYPE_INT_RGB); 

到:

private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage, BufferedImage.TYPE_INT_ARGB); 

你设置了一个变量TYPE_INT_RGBnull,所以当你制作一个缓冲图像时,它会抛出空指针异常。

TYPE_INT_RGBpublic static final变量BufferedImagePublic意味着你可以在你的object访问它,static意味着它是一个变量class不是object变量,final意味着它的TS总是相同的值e.g如果10它将永远是10

BufferedImage.TYPE_INT_RGB您可以访问它。