【问题标题】:NullPointerException when calling Graphics.drawImage [duplicate]调用 Graphics.drawImage 时出现 NullPointerException [重复]
【发布时间】:2014-05-18 09:36:23
【问题描述】:

我在运行我的 java 代码时遇到 NullPointerException。

此方法中的两行都出现错误:

    public void draw(Graphics g){
    g.drawImage(bg, 0, 0, 50, 0, null);
    g.drawImage(a.getImage(), 0, 0, 50, 0, null);
}

bg 是这样声明的:

 bg = new ImageIcon("D:\\Eclipse\\Workspace\\Game\\src\\GameTest1\\back.png").getImage();

这是这样的:

 private Animations a;

代码:

package GameTest1;

import java.awt.Image;
import java.util.ArrayList;

public class Animations {

    private ArrayList scenes;
    private int sceneIndex;
    private long movieTime;
    private long totalTime;

    //CONSTRUCTOR
    public Animations(){
        scenes = new ArrayList();
        totalTime = 0;
        start();
    }
    //adds screen to array list and adds time to things
    public synchronized void addScene(Image i, long t){ 
        totalTime += t;
        scenes.add(new OneScene(i, totalTime) );
    }
    //start animation from beginning
    public synchronized void start(){
        movieTime = 0;
        sceneIndex = 0;
    }
    //change scenes
    public synchronized void update(long timePassed){
        if(scenes.size() > 1){
            movieTime += timePassed;
            if(movieTime >= totalTime){
                movieTime = 0;
                sceneIndex = 0;
            }
            while(movieTime > getScene(sceneIndex).endTime){
                sceneIndex++;
            }
        }
    }
    //get current scene(image)
    public synchronized Image getImage(){
        if(scenes.size() == 0){
            return null;
        }else{
            return getScene(sceneIndex).pic;
        }
    }
    //get scene
    private OneScene getScene(int x){
        return (OneScene)scenes.get(x);
    }
    /////////////PRIVATE CLASSCEPTION///////////////
    private class OneScene{
    Image pic;
    long endTime;

    public OneScene(Image pic, long endTime){
        this.pic = pic;
        this.endTime = endTime;
    }

}
}

我知道 nullPointerException 是什么,所以请不要将我链接到描述它是什么的帖子。我的问题是我看不到导致错误的原因。

【问题讨论】:

  • 堆栈跟踪在哪里? a 是什么?
  • "I know what a nullPointerException is, so please do not link me to a post describing what one is." -- 如果您知道 NPE 是什么,那么您应该已经知道如何调试它。你有没有测试过什么是空的?关键可能是 Graphics 对象——你是怎么得到它的?
  • @JustinJasmann private Animations a;并且 Animations 类现在在帖子中
  • 您发布了很多代码,但与您的问题无关。再说一次,你是如何调用 draw 的,你从哪里得到 Graphics 对象?再一次,你有没有测试过什么是空的?请避免因信息不足而关闭此问题。
  • @HovercraftFullOfEels 贾斯汀要求上课,我在我的主课顶部使用 CLASSNAME.run() 调用 draw,你的意思是“我在哪里得到 Graphics 对象"

标签: java image graphics nullpointerexception fullscreen


【解决方案1】:

你说:

此方法中的两行都出现错误:

public void draw(Graphics g){
    g.drawImage(bg, 0, 0, 50, 0, null);
    g.drawImage(a.getImage(), 0, 0, 50, 0, null);
}

这很清楚地意味着您传递给draw() 方法的Graphics 对象为空。检查调用draw() 的代码行,看看为什么你传递给它的Graphics 对象为空。

你真的应该阅读post的答案,这个问题将不可避免地被标记为重复。你还没有学会如何调试 NPE。

【讨论】:

  • 伟大的思想都一样。 1+
  • 我知道我还没有找到如何调试 NPE ......这就是我在这里问的原因。另外,我确实说过我知道 NPE 是什么,但我不知道是什么原因造成的。
  • @user3532547 导致 NPE 的原因是什么? Erm 在空指针上调用方法.....
【解决方案2】:

你在打电话:

public void draw(Graphics g){
    g.drawImage(bg, 0, 0, 50, 0, null);
    g.drawImage(a.getImage(), 0, 0, 50, 0, null);
}

并获得 NullPointerException。首先你必须找到抛出NPE的行,然后测试看看什么是null。例如,如果它是方法的第一行:

public void draw(Graphics g){
    System.out.println("is g null? " + (g == null));
    System.out.println("is bg null? " + (bg == null));
    g.drawImage(bg, 0, 0, 50, 0, null);
    g.drawImage(a.getImage(), 0, 0, 50, 0, null);
}

然后如果 g 为 null,则回头看看 draw 被调用的地方,看看你是如何得到 Graphics 对象的。


编辑
你说:

好的,找到我调用draw的地方:Graphics2D g = screen.getGraphics();绘制(g);

什么是屏幕?如果它是一个组件,那么你试图在它被渲染之前从它中取出图形,它将为空。无论如何,这不是您使用图形的方式。您应该按照 Swing painting tutorials 绘制 JPanel 或 JComponent 覆盖的 paintComponent 方法。

【讨论】:

  • 好的,找到我调用draw的地方:Graphics2D g = screen.getGraphics();绘制(g);
  • @user3532547:终于。请参阅编辑。
  • screen 是这个类:pastebin.com/nTZxcQUg
  • @user3532547: 因此,如果vc.getFullScreenWindow() 返回 null,您的 getGraphics() 方法将返回 null。现在,为什么 getFullScreenWindow() 为空,这是您期望的行为吗?为什么或为什么不?
猜你喜欢
  • 2018-09-26
  • 2014-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-29
  • 2015-02-13
  • 2012-01-21
  • 2012-08-29
相关资源
最近更新 更多