日期:2014-05-20  浏览次数:20791 次

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
public class hao {
  public static void main(String args[])
  {int month;
  month=Short.parseShort(args[0]);
  switch(month)
  {case 1:System.out.println("January");break;
  case 2:System.out.println("February");break;
  case 3:System.out.println("March");break;
  case 4:System.out.println("April");break;
  case 5:System.out.println("May");break;
  case 6:System.out.println("June");break;
  case 7:System.out.println("July");break;
  case 8:System.out.println("August");break;
  case 9:System.out.println("September");break;
  case 10:System.out.println("Octember");break;
  case 11:System.out.println("November");break;
  case 12:System.out.println("December");break;
  }
  }
  }
我运行了这个程序 编译时 生成成功 但是执行时却出现
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
  at hao.main(hao.java:4)

请问这是怎么回事啊 小弟现在很迷茫 是下载的jdk有问题 还是别的原因 望赐教
谢谢

------解决方案--------------------
运行的时候有没有加参数?没有的话,到哪里去拿args[0]
------解决方案--------------------
要么在运行前配制参数,要么在运行时读取输入的参数,没有参数args[0]肯定会数组越界拉!
------解决方案--------------------
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 
-->这个是数组下标越界的运行时错误

month=Short.parseShort(args[0]); 语句中用到了args[0],即接受从键盘传入的String参数。
在运行该类时,应该在后面加上参数,比如:java hao August

------解决方案--------------------
数组下标越界,你没有参数.
------解决方案--------------------
数组是空的,当然出现越界了,你给数组赋值就好了
------解决方案--------------------
调用以前先判断 args[]是不是null...