zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    20210123
    20210122
    20210121
    20210120
    2020119
    20210118
    20210117
    20210116
    例2-8
    例2-6
  • 原文地址:https://www.cnblogs.com/followyourdream/p/3528571.html
Copyright © 2011-2022 走看看