java - 由于 java.lang.ArrayIndexOutOfBoundsException 无法打开图像

标签 java

已经制作了这个程序,应该在我的桌面上打开图像,但是当我运行它时,它给了我错误java.lang.ArrayIndexOutOfBoundsException。我做错了什么?

import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;


public class Image {
public static void main(String[] args) throws Exception
{
    new Image(args[0]);
}

public Image(final String filename) throws Exception
{
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            JFrame editorFrame = new JFrame("C:/Users/Username/Desktop/index.jpg");
            editorFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

            BufferedImage image = null;
            try
            {
                image = ImageIO.read(new File(filename));
            }
            catch (Exception e)
            {
                e.printStackTrace();
                System.exit(1);
            }
            ImageIcon imageIcon = new ImageIcon(image);
            JLabel jLabel = new JLabel();
            jLabel.setIcon(imageIcon);
            editorFrame.getContentPane().add(jLabel, BorderLayout.CENTER);

            editorFrame.pack();
            editorFrame.setLocationRelativeTo(null);
            editorFrame.setVisible(true);
        }
    });
}

我收到此错误是怎么回事:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Image.main(Image.java:15)

最佳答案

您必须将命令行参数传递给 main() 方法。我们通过 Image() 构造函数传递该参数。

new Image(args[0]);

请查看下面的命令行概念。

什么是命令行参数?

A Java application can accept any number of arguments from the command line. Those arguments called as Command Line Arguments. This allows the user to specify configuration information when the application is launched.

如何传递命令行参数?

The user enters command-line arguments when invoking the application and specifies them after the name of the class to be run.

例如, 假设一个名为 Sort 的 Java 应用程序对文件中的行进行排序。要对名为 Friends.txt 的文件中的数据进行排序,用户可以输入:

java Sort friends.txt

此处:排序--类名
friends.txt --- 命令行参数。

When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of Strings. In the previous example, the command-line arguments passed to the Sort application in an array that contains a single String: "friends.txt".

更新:

您可以传递多个命令行参数,并以空格作为分隔符。

喜欢

java Sort stack.txt overflow.txt 

如何在 Eclipse 中传递命令行参数?

转到程序包资源管理器或导航器 View 上的程序

选择该程序 ---> 右键单击​​程序后选择运行方式 ---> 运行配置 ---> 选择参数 顶部的第二个选项 ---> 在程序参数上给出参数

请参阅下面的屏幕截图:

enter image description here

关于java - 由于 java.lang.ArrayIndexOutOfBoundsException 无法打开图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31248848/

相关文章:

java - 在 JDBC 中停止或终止长时间运行的查询

java - 如何从地理编码 url 获取状态元素

java 打印数组并出现 Arrays.toString() 错误

java - 如何在java中继续使用泛型

java - 如何将数组字节转换为 org.w3c.dom.Document

java - spring框架如何知道如何在这个例子中实例化RestHighLevelClient?

java - 如何在 Java 中使用 Cassandra Map 类型

java - 如何在 Java 或 C# 等静态类型语言中避免参数顺序依赖

java - 使用正则表达式匹配查询字符串中的重复模式

java - 在值对象中存储常量