迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

Java 异常 Exception in Thread AWT-EventQueue-0 java.lang.NullPointerException

作者:迹忆客 最近更新:2023/07/16 浏览次数:

当我们使用 Java AWT 包方法并且将 null 值传递给任何方法时,会发生 “AWT-EventQueue-0”java.lang.NullPointerException 异常。 本教程演示如何在 Java 中解决此 NullPointerException。


Java 中线程“AWT-EventQueue-0”java.lang.NullPointerException 中的异常

当我们向 AWT 包传递 null 值时,会发生“AWT-EventQueue-0”java.lang.NullPointerException。 NullPointerException 异常是 Java 中最常见的异常。

当满足以下任一条件时,将发生 NullPointerException。

  1. 当访问和修改空对象字段时。
  2. 当我们从空对象调用方法时。
  3. 当访问和修改空对象的槽时。
  4. 获取任何空数组的长度时。
  5. 当我们尝试同步空对象时。
  6. 当我们抛出空值时。

让我们尝试一个在 Java 中抛出“AWT-EventQueue-0”java.lang.NullPointerException 的示例。

package jiyik;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Timer;

@SuppressWarnings("serial")

public class Example extends JFrame implements ActionListener , KeyListener {

    static Dimension Screen_Size = new         Dimension(Toolkit.getDefaultToolkit().getScreenSize());
    Insets Scan_Max = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());
    int Task_Bar_Size = Scan_Max.bottom;
    static JFrame Start_Screen = new JFrame("Start Screen");
    static JFrame Game_Frame = new JFrame("Begin the Game!");
    static JLabel Cow_Label = new JLabel();
    static int Sky_Int = 1;
    static JLabel Sky_Label = new JLabel();
    static int SECONDS = 1;
    static boolean IS_Pressed = false;
    public static void main(String[] args) {
        new Example();

    }
    public Example() {

        JPanel Buttons_Panel = new JPanel();
        Buttons_Panel.setLayout(null);
        Start_Screen.setSize(new Dimension(Screen_Size.width - getWidth(), Screen_Size.height - Task_Bar_Size - getHeight()));
        Start_Screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Start_Screen.setVisible(true);
        System.out.println(Start_Screen.getSize());

        //buttons

        JButton Start_Button = new JButton("Start");
        Start_Button.addActionListener(this);
        Start_Button.setSize((int) Start_Screen.getWidth()/7, (int) (Start_Screen.getHeight()/15.36));
        Start_Button.setBounds((Start_Screen.getWidth()/2) - Start_Button.getWidth()/2,((int)Start_Screen.getHeight()/2) - Start_Button.getHeight(),Start_Button.getWidth(),Start_Button.getHeight());
        Start_Button.setActionCommand("Start");

        Buttons_Panel.add(Start_Button);
        Start_Screen.add(Buttons_Panel);
    }
    @Override
    public void actionPerformed(ActionEvent Action_Event) {
        Object CMD_Object =  Action_Event.getActionCommand();
        if(CMD_Object == "Start") {
            Start_Screen.setVisible(false);
            //  getClass().getResource("/cow.png") and getClass().getResource("/grass.png") is giving null
            // because there is no image in folder named cow.png or grass.png
            ImageIcon Cow_Image = new ImageIcon(getClass().getResource("/cow.png"));
            ImageIcon Grass_Image = new   ImageIcon(getClass().getResource("/grass.png"));

            Game_Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Game_Frame.setSize(Start_Screen.getSize());
            Game_Frame.setVisible(true);
            JPanel Demo_Panel = new JPanel();
            Demo_Panel.setBackground(Color.white);
            Demo_Panel.setLayout(null);
            Demo_Panel.setFocusable(true);
            Game_Frame.add(Demo_Panel);
            Demo_Panel.addKeyListener(this);
            Cow_Label.setBounds( (Start_Screen.getWidth()/2)-105, (Start_Screen.getHeight()/2)-55, 210, 111);
            Cow_Label.setIcon(Cow_Image);
            Demo_Panel.add(Cow_Label);
            Demo_Panel.setVisible(true);
            Cow_Label.setVisible(true);
            JLabel Grass_Label = new JLabel();
            System.out.println("grass");
            //  getClass().getResource("/Sky.png") will throw a nullpointerexception because there is no image in the folder
            ImageIcon Sky1 = new ImageIcon(getClass().getResource("/Sky.png"));
            Sky_Label.setIcon(Sky1);
            Grass_Label.setIcon(Grass_Image);
            Grass_Label.setBounds(0, ( Start_Screen.getHeight()-308), Start_Screen.getWidth(), 350);
            System.out.println("mOooow");
            Demo_Panel.add(Grass_Label);
            Sky_Label.setBounds(1, 56, 1366, 364);
            Demo_Panel.add(Sky_Label);
            System.out.println("google");
        }

    }
    @Override
    public void keyPressed(KeyEvent Key_Event) {
        int CMD_Int = Key_Event.getKeyCode();
    //  getClass().getResource("/cow moving.gif") will throw a nullpointerexception because there is no image in the folder
        ImageIcon Moving_Cow = new ImageIcon(getClass().getResource("/cow moving.gif"));
        System.out.println(CMD_Int);
        IS_Pressed = true;
        if(CMD_Int == 39){
            System.out.println("Key is Pressed");
            Cow_Label.setIcon(Moving_Cow);
        }
        else if(CMD_Int == 37){

        }
        System.out.println("End");
        while(IS_Pressed==true){
            Timer Wait_Please = new Timer("Wait Please");
            try {
                Wait_Please.wait(1000);
            }
            catch(InterruptedException p){}
            int SKY = 1;
            SKY += 1;
            String SKY_String = "/Sky" + String.valueOf(SKY) + ".png";
            ImageIcon SKy = new ImageIcon(getClass().getResource(SKY_String));
            Sky_Label.setIcon(SKy);
            if(IS_Pressed==false){
            Wait_Please.cancel();
            break;
            }
        }
    }



    @Override
    public void keyReleased(KeyEvent Key_Event) {
    //  getClass().getResource("/cow.png") and getClass().getResource("/grass.png") is giving null
        // because there is no image in folder named cow.png or grass.png
        ImageIcon Cow_Image = new ImageIcon(getClass().getResource("/cow.png"));
        int CMD_Int = Key_Event.getKeyCode();
        IS_Pressed = false;
        if(CMD_Int == 39){
            Cow_Label.setIcon(Cow_Image);
        }
        else if(CMD_Int == 37){
            Cow_Label.setIcon(Cow_Image);
        }
    }
    @Override
    public void keyTyped(KeyEvent c) {
        // TODO Auto-generated method stub
    }
}

上面的代码是关于一个简单的游戏,一头牛站着,按下按钮时牛就会开始移动。 它将抛出“AWT-EventQueue-0”java.lang.NullPointerException,因为 AWT 方法 new ImageIcon(getClass().getResource()) 正在获取 null 条目。

该代码的输出是:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "location" is null
    at java.desktop/javax.swing.ImageIcon.<init>(ImageIcon.java:234)
    at jiyik.Example.actionPerformed(Example.java:48)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
...

我们可以通过将图像移动到类文件夹路径来解决这个问题。 我们还可以删除 /,因为 Windows 在 Java 中使用 \ 作为路径。

如果仍然不起作用,我们可以提供图像的完整路径。 进一步的解释在上面的代码中被注释掉了。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

JavaScript POST

发布时间:2024/03/23 浏览次数:96 分类:JavaScript

本教程讲解如何在不使用 JavaScript 表单的情况下发送 POST 数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便