getGraphics()上的空指针异常 [英] Null Pointer Exception on getGraphics()

查看:465
本文介绍了getGraphics()上的空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序看起来像这样,我在draw()方法得到一个空指针异常,确切地说是g.drawImage(img,0,0,null)

my application looks like that, i am getting a null pointer exception at the draw() method, to be exact at g.drawImage(img, 0, 0, null)

package com.ochs.game;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JPanel implements Runnable{
private static final long serialVersionUID = 8229934361462702491L;

public static final int WIDTH = 320;
public static final int HEIGHT = 240;
public static final int SCALE = 2;

public boolean isRunning;

private BufferedImage img;
private Graphics2D g2d;

public Game() {
    setFocusable(true);
    requestFocus();
    start();
}

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

public void stop() {
    isRunning = false;
}

public void run() {
    long start;
    init();
    while(isRunning) {
        start = System.currentTimeMillis();

        update();
        render();
        draw();

        try {
            Thread.sleep(5 - (System.currentTimeMillis() - start));
        } catch (Exception e) {
        }
    }
}

public void init() {
    img = new BufferedImage(WIDTH*SCALE, HEIGHT*SCALE, BufferedImage.TYPE_INT_RGB);
    g2d = (Graphics2D) img.getGraphics();
}

public void update() {

}

public void render() {

}

public void draw() {
    Graphics g = getGraphics();
    g.drawImage(img, 0, 0, null);    // <<<<< getting null pointer here!
}

public static void main(String[] args) {
    Dimension size = new Dimension(WIDTH*SCALE, HEIGHT*SCALE);
    Game gameComponent = new Game();
    JFrame frame = new JFrame();
    frame.setVisible(true);
    frame.setSize(size);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(gameComponent);
}
}

现在我的问题是:为什么我得到一个null尝试绘制名为img的bufferedimage时指针异常?我也尝试使用drawString()输出一些字符串,但这也给了我一个nullpointerexception。
有没有人有建议?

Now my question is: why do i get a null pointer exception when trying to draw the bufferedimage called img? I also tried just outputting some string by using drawString() but this just gives myself a nullpointerexception, too. does anyone has an advice?

推荐答案

你可能试图通过获取图形上下文getGraphics()在渲染JPanel之前,该方法返回null。不要这样做。在组件上使用 getGraphics()来获取Graphics上下文存在问题,其中一个是您在上面看到的问题,另一个是获得的Graphics上下文不会坚持下去。有时这是必要的,但通常我们通过 paintComponent(...)进行被动绘图。 Swing Timer通常可以用于动画循环。

You're likely trying to get the Graphics context via getGraphics() before the JPanel has been rendered, and thus the method returns null. Don't do this. There are problems with using getGraphics() on a component to get the Graphics context, one of which is the problem you're seeing above, and another is that the Graphics context obtained will not persist. There are occasions when this is necessary to do, but usually we do passive drawing via paintComponent(...). Often a Swing Timer can be used for the animation loop.

这篇关于getGraphics()上的空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆