线程“AWT-EventQueue-0”ERROR中的异常

时间:2014-03-01 03:53:40

标签: java swing nullpointerexception paint

我试图通过paint方法将背景绘制到窗口,看起来我已经成功了。但是我在控制台上发现了一个问题。那就是" NullPointerException"。

GameWindow代码:

package com.vboar.game;

import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;

public class GameWindow extends JFrame {

private List<BackGround> allBg = new ArrayList<BackGround>();
private BackGround nowBg = null;

public GameWindow() {
    this.setTitle("IGame Beta v1.0");
    this.setSize(1000, 600);
    int width = Toolkit.getDefaultToolkit().getScreenSize().width;
    int height = Toolkit.getDefaultToolkit().getScreenSize().height;
    this.setLocation((width - 1000)/2, (height - 600)/2);
    this.setVisible(true);
        StaticValue.init();
    for (int i = 1; i <= 10; i++) {
        this.allBg.add(new BackGround(i));
    }
    this.nowBg = this.allBg.get(0);
    this.repaint();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setResizable(false);
}

public static void main(String[] args) {
    new GameWindow();
}

@Override
public void paint(Graphics g) {
    BufferedImage image = new   BufferedImage(1000,600,BufferedImage.TYPE_3BYTE_BGR);
    Graphics g2 = image.getGraphics();
    g2.drawImage(this.nowBg.getBgImage(), 0, 0, this);
    g.drawImage(image, 0, 0, this);
}
}


BackGround代码:

package com.vboar.game;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

public class BackGround {
private BufferedImage bgImage = null;
private int sort;
public BufferedImage getBgImage() {
    return bgImage;
}
public BackGround(int sort) {
    this.sort = sort;
    bgImage = StaticValue.allbgImage.get(0);
}
}

StaticValue代码:

package com.vboar.game;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

public class StaticValue {
public static BufferedImage blockImage = null;
public static BufferedImage treeImage = null;
public static BufferedImage startImage = null;
public static BufferedImage deadImage = null;
public static List<BufferedImage> allbgImage = new ArrayList<BufferedImage>();
public static List<BufferedImage> alliImage = new ArrayList<BufferedImage>();
public static String imagePath = System.getProperty("user.dir") + "/bin/";

public static void init() {
    try {
        blockImage = ImageIO.read(new File(imagePath + "block.png"));
        treeImage = ImageIO.read(new File(imagePath + "greentree.png"));
        startImage = ImageIO.read(new File(imagePath + "startbg.png"));
        deadImage = ImageIO.read(new File(imagePath + "9.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    for (int i = 1; i <= 8; i++) {
        try {
            alliImage.add(ImageIO.read(new File(imagePath  + i + ".png")));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    for (int i = 1; i <= 10; i++) {
        try {
            allbgImage.add(ImageIO.read(new File(imagePath  + "background" + i + ".png")));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}

完整的异常stackTrace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException {
at com.vboar.game.GameWindow.paint(GameWindow.java:41)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
}

这是我第一次在stackoverflow.com上询问,以致代码可能非常难看。另外,我不是说英语的人,而且写得很糟糕。能帮助我,一个Java学习者,来解决这个问题吗?

提前谢谢。

Vboar

1 个答案:

答案 0 :(得分:1)

在进行一些测试之后,似乎导致的错误是由于您第一次尝试在窗口上绘制图像时尚未加载您尝试绘制的图像。

swing组件中的错误处理会为你处理错误,虽然你应该在你绘制图像的位置放置一个try catch语句来自己捕获这个错误,如下所示:

try {
    g2.drawImage(this.nowBg.getBgImage(), x, 0, this);
} catch (Exception e) {
    e.printStackTrace(); //optional for seeing the error message
}

在查看您的代码之后,我提出了一些其他建议,可以帮助您完成当前的计划和编程技能。

  1. 除非是变量,否则不要包含公共变量和静态变量 将是最终的或只读的。这是一个通用的编程规则 适用于多种语言,通常会使实施和维护更复杂的程序变得更加困难。

  2. 在GameWindow中创建背景类时 class你基本上复制了加载的图像 StaticValue类。这实际上是加载时间 更长时间并增加内存使用量而无法获得。尝试仅在一个类中加载和使用图像,而不是多个。