java 中 java.lang.ArrayIndexOutOfBoundsException: 0 异常

 1 package test;
 2 
 3 public class Test {
 4 
 5     public static void main(String[] args) {
 6 
 7         final int num2 = Integer.parseInt(args[0]);
 8 
 9     }
10 
11 }

编译时,会报
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

posted @ 2014-01-21 16:27  xuefenhu  阅读(6375)  评论(0编辑  收藏  举报