2015-09-12 29 views
0

我试图在JFrame上画一个简单的矩形,但是当我试图绘制它时,它给了我一个NullPointerException。我无法找到问题,因为这是我已经使用的代码之一。 Null的对象是我从超类获得的Graphics对象。有人可以帮我弄这个吗?Java - NullPointerException与图形

错误:

Exception in thread "Thread-2" java.lang.NullPointerException 
at com.daansander.game.Game.render(Game.java:83) 
at com.daansander.game.Game.run(Game.java:76) 
at java.lang.Thread.run(Thread.java:745) 

全码:

package com.daansander.game; 


import javax.swing.*; 
import java.awt.*; 
import java.awt.image.BufferStrategy; 

/** 
* Created by Daan on 12-9-2015. 
*/ 
public class Game extends Canvas implements Runnable { 

    public static final int WIDTH = 500; 
    public static final int HEIGHT = WIDTH/12 * 9; 
    public static final int SCALE = 2; 
    public static final String NAME = "2DGame"; 

    private JFrame frame; 
    private volatile boolean running; 

    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 static void main(String[] args) { 
     new Game().start(); 
    } 

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

    public synchronized void stop() { 
    } 

    @Override 
    public void run() { 
     long lastTime = System.currentTimeMillis(); 
     long current; 
     long delta = 0; 

     int frames = 0; 
//  int ticks = 0; 

     while (running) { 
      current = System.currentTimeMillis(); 
      delta += current - lastTime; 
      lastTime = current; 
      frames++; 

      if (delta > 1000) { 
       delta -= 1000; 
       System.out.println("FRAMES " + frames); 
       frames = 0; 

      } 

      try { 
       Thread.sleep(2); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

      render(); 
     } 
    } 

    public void render() { 
     BufferStrategy bs = getBufferStrategy(); 

     Graphics g = bs.getDrawGraphics(); // <----- Line of error 

//  g.setColor(Color.black); 
//  g.drawRect(0, 0, 5, 5); 
// 
//  g.dispose(); 
//  bs.show(); 
    } 

    public void tick() { 

    } 

    public void init() { 

    } 
} 
+1

http://docs.oracle.com/javase/7/docs/api/java/awt/Canvas.html#getBufferStrategy%28%29 – fabian

+0

@fabian THX解决了它 –

+0

这可能会发生,因为屏幕上还没有实现“画布”(即连接到本地对等体)。你应该在你的代码中为它做出调整。在调用'getDrawGraphics'之前,确保你还在'Canvas'上调用'createBufferStrategy' – MadProgrammer

回答

2

getBufferStrategy()被返回null。作为费边linked to above

public BufferStrategy getBufferStrategy()

Returns the BufferStrategy used by this component. This method will return null if a BufferStrategy has not yet been created or has been disposed.

You need to create a BufferStrategy first.