【问题标题】:Error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Src.main(Src.java:6)线程“main”中的错误异常 java.lang.ArrayIndexOutOfBoundsException: 0 在 Src.main(Src.java:6)
【发布时间】:2014-08-03 20:11:23
【问题描述】:

我正在使用这个程序并收到上述错误!

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

【问题讨论】:

  • 提供更多细节,你是如何执行代码的……
  • @shekharsuman 我在 Eclipse 上运行它...
  • @AbhishekKumarRao-您显然需要在 Eclipse 中为 main 方法设置参数! eclipse中有一个单独的过程。在 google.com 上搜索!

标签: java exception indexoutofboundsexception


【解决方案1】:

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

java Src arg0 arg1

args 是一个String[],它填充了调用程序时传递的命令行值,默认情况下它是空的,因此会抛出一个ArrayIndexOutOfBoundsException,因为索引0 处没有任何内容。

【讨论】:

    【解决方案2】:

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

    args[] 数组包含在命令行启动程序时传递的参数,也许你没有传递参数。

    您可以在此处找到 ArrayIndexOutOfBoundException 的示例:https://stackoverflow.com/a/5554781/2649618

    这是 Oracle 网站上的官方文档页面(适用于 Java 7):http://docs.oracle.com/javase/7/docs/api/java/lang/ArrayIndexOutOfBoundsException.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多