2014-10-29 88 views
-1

這段代碼有什麼問題?
我不斷收到錯誤
NullPointerException - 我不明白

Exception in thread "Thread-2" java.lang.NullPointerException 
at GUI.render(GUI.java:68) 
at GUI.run(GUI.java:51) 

請幫助

import graphics.*; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.Canvas; 
import java.awt.image.BufferStrategy; 
import java.awt.image.BufferedImage; 
import java.awt.image.DataBufferInt; 

public class GUI extends Canvas implements Runnable { 
    private static final long serialVersionUID = 1L; 
    final static String LABEL_TEXT = "Game"; 
    public static final int Width = 1020; 
    public static final int Height = 680; 
    private Thread thread; 
    private Screen screen; 
    private Render render; 
    private BufferedImage img; 
    private boolean running = false; 
    private int[] pixels; 

    public void display() { 
     screen = new Screen(Height, Width); 
    vimg = new BufferedImage(Width, Height, BufferedImage.TYPE_INT_RGB); 
    pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData(); 
    } 

    private void start() { 
     if(running) 
      return; 
     running = true; 
     thread = new Thread(this); 
     thread.start(); 
    } 

    private void stop() { 
     if(!running) 
      return; 
     running = false; 
     try { 
      thread.join(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      System.exit(0); 
     } 
    } 

    public void run() { 
     while (running){ 
      tick(); 
      render(); 
     } 
    } 

    public void tick() { 

    } 

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

     screen.render(); 

     for (int i = 0; i <Width*Height; i++){ 
      pixels[i] = screen.pixels[i]; 
     } 
     Graphics g = bs.getDrawGraphics(); 
     g.drawImage(img, 0, 0, Width, Height, null); 
     g.dispose(); 
     bs.show(); 
    } 


    /** 
    * Create and show the GUI. 
    */ 
    public static void main(String[] args) { 
     /*Create Canvas*/ 
     GUI game = new GUI(); 

     /*Create and set up the frame*/ 
     JFrame frame = new JFrame("GUI"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     /*Add content pane to frame*/ 
     frame.add(game); 

     /*Size and then display the frame.*/ 
     frame.setSize(Width, Height); 
     frame.setVisible(true); 
     frame.setLocationRelativeTo(null); 

     game.start(); 
    } 
} 
package graphics; 

public class Render { 
    public final int width; 
    public final int height; 
    public int[] pixels; 

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

    public void draw(Render render, int xOffset, int yOffset) { 
     for (int y =0; y<height; y++) { 
      int yPix = y + yOffset; 
      for (int x =0; x<width; x++) { 
       int xPix = x + xOffset; 

       pixels[xPix+yPix*width] = pixels[x+y*width]; 
      } 
     } 
    } 
} 


package graphics; 

import java.util.Random; 
import graphics.Render; 

public class Screen extends Render{ 

    private Render test; 

    public Screen(int width, int height) { 
     super(width, height); 
     Random rand = new Random(); 
     test = new Render(256, 256); 
     for (int i =0; i< 256*256; i++){ 
      test.pixels[i] = rand.nextInt(); 
     } 
    } 

    public void render() { 
     draw(test, 0, 0); 
    } 
} 
+0

NPE在哪裏?我不喜歡數線 – msrd0 2014-10-29 18:21:40

+0

那麼,GUI.java的第68行是哪裏? – 2014-10-29 18:22:01

+0

'pixels [i] = screen.pixels [i];'是第68行,對吧? – 2014-10-29 18:22:20

回答

0

您還沒有叫display()在使用之前初始化變量。

+0

'test = new Render(256,256);'在構造函數中... – 2014-10-29 18:24:33

+0

查看我上面更新的答案。 – 2014-10-29 20:17:46

+0

非常感謝你 – 2014-10-29 23:44:25