简体   繁体   English

线程“ AWT-EventQueue-0”中的异常java.lang.IllegalArgumentException:输入== null

[英]Exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException: input == null

I want to display a random image in this program when the button is clicked but i get an exception error and cant fix it. 单击按钮时,我想在此程序中显示随机图像,但出现异常错误,无法修复。

public class TestImage1 {

    public static void main(String args[]) throws IOException {
        String sonido = "Windows Navigation Start.wav";
        InputStream in = new FileInputStream(sonido);
        AudioStream audio = new AudioStream(in);
        AudioPlayer.player.start(audio);
        try {
            TestImage1 obj = new TestImage1();
            obj.run(args);
        } catch (Exception e) {
        }
    }

    public void run(String[] args) throws Exception {
        imgRandom form = new imgRandom();
        form.setBounds(0, 0, 500, 400);
        form.setVisible(true);
    }

    public class imgRandom extends JFrame implements ActionListener {

        private JLabel label2;
        JButton boton;
        String cads[] = {"/img/duck.gif", "/img/luigi.png", "/img/mario.jpg"};

        public imgRandom() throws IOException {
            setLayout(null);
            label2 = new JLabel("press");
            label2.setBounds(10, 20, 100, 100);
            add(label2);

            boton = new JButton("Presioname!!");
            boton.setBounds(150, 250, 200, 100);
            add(boton);
            boton.addActionListener(this);

        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == boton) {
                int ra = (int) (Math.random() * 3);
                URL url = this.getClass().getResource(cads[ra]);
                try {
                    Image img = ImageIO.read(url);
                    label2.setIcon(new ImageIcon(img));
                } catch (IOException e1) {
                    e1.printStackTrace();
                }

            }
        }
    }
}

can sombedy tell me how to fix it. 可以卑鄙地告诉我如何解决它。 if the button is pressed it should display a random image but it throws an exception with a lot of other error. 如果按下按钮,它将显示一个随机图像,但会引发异常,并带有许多其他错误。

In your code there might be some issues while getting the images. 在您的代码中,获取图像时可能会出现一些问题。

I have already posted some answers in the same context that might help you. 我已经在可能对您有所帮助的同一上下文中发布了一些答案。


Some points: 一些要点:

  1. Don't use null layout instead use proper layout that's why they are designed for. 不要使用null布局,而要使用正确的布局,这就是为什么要为其设计的原因。

    Read more How to Use Various Layout Managers 阅读更多信息如何使用各种布局管理器

  2. Use SwingUtilities.invokeLater() to make sure that EDT is initialized properly. 使用SwingUtilities.invokeLater()确保EDT正确初始化。

    Read more 阅读更多

  3. Don't extend any class until and unless you are modifying the existing logic. 除非修改现有逻辑,否则不要扩展任何类。

    Read more 阅读更多

暂无
暂无

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

相关问题 线程“ AWT-EventQueue-0”中的java异常java.lang.IllegalArgumentException - java exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException 线程“ AWT-EventQueue-0”中的异常java.lang.IllegalArgumentException: - Exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException: 获取错误-线程“ AWT-EventQueue-0”中的异常java.lang.IllegalArgumentException:文本不能为null或为空 - Getting error - Exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException: text cannot be null or empty 线程“AWT-EventQueue-0”中的异常java.lang.IllegalArgumentException:im == null - Exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException: im == null 线程“ AWT-EventQueue-0”中的异常java.lang.IllegalArgumentException:矩形的宽度和高度必须大于0 - Exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException: Rectangle width and height must be > 0 线程“AWT-EventQueue-0”中的异常java.lang.IllegalArgumentException:比较方法违反了其总契约 - Exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException: Comparison method violates its general contract 线程“ AWT-EventQueue-0”中的异常java.lang.IllegalArgumentException:该文件不存在 - Exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException: The file doesn't exist 线程“AWT-EventQueue-0”中的异常 java.lang.IllegalArgumentException: 宽度 (-1) 和高度 (-1) 不能 &lt;= 0 - Exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0 线程“ AWT-EventQueue-0”中的异常java.lang.IllegalArgumentException:大小无效 - Exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException: Invalid size 线程“ AWT-EventQueue-0”中的异常java.lang.NumberFormatException:对于输入字符串:“ null” - Exception in thread “AWT-EventQueue-0” java.lang.NumberFormatException: For input string: “null”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM