简体   繁体   English

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

[英]Exception in thread “AWT-EventQueue-0” ERROR

I have tried to paint the background to the window via the paint method and it seems I have succeeded.But I found a problem on the console.That's "NullPointerException". 我试图通过paint方法将背景绘制到窗口上,似乎已经成功了。但是我在控制台上发现了一个问题。那就是“ NullPointerException”。

The GameWindow Code: 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);
}
}


The BackGround Code: 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);
}
}

The StaticValue Code: 静态值代码:

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();
        }
    }
}
}

The full exception stackTrace: 完整的异常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)
}

It's my first time to ask on the stackoverflow.com so that the codes may be very ugly. 这是我第一次在stackoverflow.com上提问,以使代码可能非常难看。 Also, I'm not an English speaker and I have made terrible writing. 另外,我不是说英语的人,我的写作也很糟糕。 Could you please help me,a Java learner,to solve this problem? Java学习者,能否请您帮我解决这个问题?

Thank you in advance. 先感谢您。

Vboar Vboar

After doing some tests it seems that the error you are getting is caused because the image you are trying to draw has not been loaded by the first time you try to draw the image on the window. 经过一些测试之后,您似乎得到的错误似乎是由于您试图绘制的图像在您第一次尝试在窗口上绘制图像时尚未加载而引起的。

The error handling within the swing components will handle the error for you though you should put a try catch statement around where you draw the image to catch this error yourself, like so: 尽管您应该在绘制图像的位置附近放置try catch语句来自己捕获此错误,但是swing组件内的错误处理将为您处理错误,如下所示:

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

After looking at you code there are a few other recommendations that I came up with that can help your current program and your programming skills overall. 在查看了您的代码之后,我提出了一些其他建议,这些建议可以帮助您改善当前的程序和整体的编程技能。

  1. Don't have variables that are both public and static unless its going to be final or readonly. 除非变量为final或readonly,否则不要同时具有public和static变量。 It's a general programming rule that applies to multiple languages and generally makes it tougher to implement and maintain a more complex program. 这是一条通用的编程规则,适用于多种语言,通常使实施和维护更复杂的程序变得更加困难。

  2. When you are creating the background classes within your GameWindow class you are essentially duplicating the images that were loaded in the StaticValue class. 当您在GameWindow类中创建背景类时,实际上是在复制在StaticValue类中加载的图像。 This essentially makes the loading time longer and increases memory usage for no gain. 从本质上讲,这会延长加载时间,并增加了内存使用量,但没有增加。 Try loading and using the images within only a single class rather than multiple. 尝试仅在一个类而不是多个类中加载和使用图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 解析错误-线程“ AWT-EventQueue-0”中的异常 - Parsing error - Exception in thread “AWT-EventQueue-0” 在“ AWT-EventQueue-0”线程错误中获取异常 - Getting Exception in “AWT-EventQueue-0” Thread Error 线程“AWT-EventQueue-0”中的异常? - Exception in thread “AWT-EventQueue-0”? 错误:线程“ AWT-EventQueue-0”中的异常java.lang.ClassCastException: - Error :Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: 错误:线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException - ERROR : Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException 线程“ AWT-EventQueue-0”中的异常java.lang.ArrayIndexOutOfBoundsException:-1错误 - Exception in thread “AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException: -1 error 线程“ AWT-EventQueue-0”中的异常java.lang.ClassCastException错误 - Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException error 错误“线程“AWT-EventQueue-0”中的异常java.lang.NumberFormatException” - Error "Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException" 线程“AWT-EventQueue-0 不支持的操作”中的异常 - Exception in thread "AWT-EventQueue-0 Unsupported operation 线程“ AWT-EventQueue-0”和奇数TextArea中的异常 - Exception in thread “AWT-EventQueue-0” and odd TextArea
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM