Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
线程“main”java.lang.ArrayIndexOutOfBoundsException中出现错误异常:0位于Src.main(Src.java:6)_Java_Exception_Indexoutofboundsexception - Fatal编程技术网

线程“main”java.lang.ArrayIndexOutOfBoundsException中出现错误异常:0位于Src.main(Src.java:6)

线程“main”java.lang.ArrayIndexOutOfBoundsException中出现错误异常:0位于Src.main(Src.java:6),java,exception,indexoutofboundsexception,Java,Exception,Indexoutofboundsexception,我正在使用这个程序,并得到上述错误 public class Src { public static void main(String args[]) { // TODO Auto-generated method stub int a=Integer.parseInt(args[0]); int b=Integer.parseInt(args[1]); int c=a+b; System.out.prin

我正在使用这个程序,并得到上述错误

public class Src {

    public static void main(String args[]) {
        // TODO Auto-generated method stub
        int a=Integer.parseInt(args[0]);
        int b=Integer.parseInt(args[1]);
        int c=a+b;
        System.out.println(""+c);
    }
}

调用程序时,必须在命令行传递一些参数:

java Src arg0 arg1

args是一个字符串[],在调用程序时用传递的命令行值填充,默认情况下为空,因此会引发ArrayIndexOutOfBoundsException,因为索引0中没有任何内容。

问题是,您试图读取args[]数组中的第一个元素,而该元素不存在

args[]数组包含在程序启动时在命令行中传递的参数,可能您没有传递参数

您可以在此处找到ArrayIndexOutOfBoundException的示例:


这是Oracle网站上针对Java 7的官方文档页面:

args可能为空。检查此项提供更多详细信息,您如何执行代码以及所有…@shekharsuman我正在eclipse上运行它…@AbhishekKumarRao您显然需要在eclipse中为main方法设置参数!在eclipse中有一个单独的过程。在google.com上搜索!