Results 1 to 4 of 4
  1. #1
    dwill19686 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

    Hi Guys,

    I'm creating this program called CommandLineArguments out of this book. Its giving me the same error as before

    "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1".

    This program is supposed to execute and output
    args.length = 3
    args[0] = 1
    args[1] = 2
    args[2] = abcde
    when I type in "java CommandLineArguments 1 2 abcde" from the command line, but stops me and lets me know that I have an error. What am I missing? Here is my code.

    public class CommandLineArguments {


    public static void main(String[] args) {

    System.out.println("args.length= " + args.length);
    System.out.println("args[0]= " + args[0]);
    System.out.println("args[1]= " + args[1]);
    System.out.println("args[2]= " + args[2]);
    }

    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You sure you aren't adding any extra arguments on the command line by mistake? I just copy and pasted your code, compiled it, and supplied the args 1 2 abcde and got

    Java Code:
    args.length= 3
    args[0]= 1
    args[1]= 2
    args[2]= abcde
    as expected.

  3. #3
    dwill19686 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    I am running it in eclipse and getting the error. Would that make a difference?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I have no experience with eclipse, but I'd imagine it requires you to do something else when using cmd args since using cmd to compile and run works fine. Someone who has experience with eclipse will be around sooner or later to tell you what you have wrong.

Similar Threads

  1. Replies: 8
    Last Post: 03-05-2011, 03:53 AM
  2. Replies: 4
    Last Post: 02-02-2011, 04:32 AM
  3. Replies: 5
    Last Post: 04-05-2010, 02:28 PM
  4. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •