You are currently offline, waiting for your internet to reconnect

PRB: ERROR: java.lang.ArrayIndexOutOfBoundsException

This article was previously published under Q173788
SYMPTOMS
Attempting to execute a Java Application without passing Command-Line orProgram Arguments, as your Java application expects, results in thefollowing error:
ERROR: java.lang.ArrayIndexOutOfBoundsException
CAUSE
This is because when the Java program accesses the "args" array for theCommand-Line or Program arguments, it tries to get items from the arraythat are out of the arrays bounds (the index used is higher than the numberof array items - 1).
RESOLUTION
To fix this problem, you need to pass in Program Arguments or Command-Linearguments when executing your Java program that expects them. The MoreInformation section below shows how to specify Program Arguments under theIDE.

You can also put a try-catch block around the code that accesses theprogram arguments. This code should catch anArrayIndexOutOfBoundsException.
STATUS
This behavior is by design.
MORE INFORMATION
When executing your Java program under the IDE that expects Command-Line orProgram arguments, you can select Settings from the Project menu, click theDebug tab, and select Category:Program arguments to set the programarguments. If you are using Visual J++ 1.0, then you can select Settingsfrom the Build menu, click the Debug tab, and select Category:ProgramArguments to set the program arguments.

Steps to Reproduce Behavior

  1. Create a Java Project and include the following code snippet in it.
  2. The following code snippet was taken from "Learn Java Now":
          import java.io.*;      class App1_2      {         public static void main(String args[])         {      // uncomment the next line to make the program catch the exception:      //  try {             int nMonth = Integer.parseInt(args[0]);          int nDay   = Integer.parseInt(args[1]);          int nYear  = Integer.parseInt(args[2]);          int nDayInYear = 0;          for (int nM = 0; nM < nMonth; nM++)          {           switch(nM)           {           case 4: case 6: case 9: case 11:              nDayInYear +=30;              break;           case 2:              nDayInYear +=28;              if (((nYear % 4) == 0) && ((nYear % 100) != 0))              {               nDayInYear++;              }              break;              default:              nDayInYear += 31;           }          }          nDayInYear += nDay;          System.out.print (nMonth + "-" + nDay + "-" + nYear);          System.out.println (" is day number "                  + nDayInYear                  + " in the year");         }      // uncomment this section to make the program catch the exception:      // } catch (ArrayIndexOutOfBoundsException e) {      //   System.out.println("This program takes 3 parameters: ");      //   System.out.println("  month day year.");      // }      }					
  3. Build the project and Execute it under the IDE.
  4. The ArrayIndexOutofBoundsException error appears.
REFERENCES
For the latest Knowledge Base articles and other support information onVisual J++ and the SDK for Java, see the following page on the MicrosoftTechnical Support site:
Properties

Article ID: 173788 - Last Review: 08/25/2005 03:41:00 - Revision: 2.1

  • Microsoft Visual J++ 1.0 Standard Edition
  • Microsoft Visual J++ 1.1 Standard Edition
  • kberrmsg kbprb kbarttypeinf KB173788
Feedback