java 中 java.lang.ArrayIndexOutOfBoundsException: 0 异常

时间:2023-03-09 16:50:26
java 中 java.lang.ArrayIndexOutOfBoundsException: 0 异常
 package test;

 public class Test {

     public static void main(String[] args) {

         final int num2 = Integer.parseInt(args[0]);

     }

 }

编译时,会报
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at test.Test.main(Test.java:7)

最终的结论是: 运行时忘了加运行时参数了。

运行时没有输入参数数据,所以args数组中还没值,如果取它的第一个值,那肯定是数组越界异常。
可以先判断:if(args.length!=0)

1、应该这样运行:java  test.Test  参数
2、如果在Eclipse中运行的话:

  run->run configurations...->argumens

  在这个里面输入运行时参数,若有多个参数以空格隔开。

参考链接:http://www.iteye.com/problems/35147