Results 1 to 7 of 7
  1. #1
    hellolleh is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default Help with error!!

    I am using openjdk on Ubuntu and I am trying to wright the following program:

    public class use
    {
    public static void main(String[] args)
    {
    System.out.print ("hi");
    System.out.println(args[0]);
    System.out.println("gal");
    }
    }

    I got the program out of a handbook, but I constantly get the following error:

    hiExeption in thread "main" java.lang.ArrayIndexOutOfBoundsExeption:0 at use.main(use.java:11)

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Ok, first of all, naming convention states that class names should begin with uppercase, just a nitpick, but it's good to get the right habits right off the bat. Now, your error pops up, because of this line:
    Java Code:
    System.out.println(args[0]);
    args stores command line parameters used when you launch the program, so for instance, if you ran a program like this:
    java MyProg one two three
    values in the args array would be as follows:
    args[0] = "one";
    args[1] = "two";
    args[2] = "three";
    You ran your program without any parameters, and so the args array was not initialised, and you tried to print out an undeclared value. To avoid this kind of problem, do this:
    Java Code:
    public class Use { //note the uppercase
       public static void main(String[] args) {
          if(args.length == 0) {
               System.out.println("Usage: java Use yourname");
               System.exit(1); //nonzero exit values mean program terminated because of error
          }
          System.out.println("hi "+args[0]+" gal");
       }
    }
    Now, if you run this program without a parameter (java Use), the output would be the usage, then the program quits, and doesn't terminate with an exception.

  3. #3
    hellolleh is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default

    If I use the program it always exits, how do I get the args.length longer so that it doesn't exit.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by hellolleh View Post
    If I use the program it always exits, how do I get the args.length longer so that it doesn't exit.
    Pass a command line argument to your class, your code expects at least one argument:

    Java Code:
    java -cp . use hellolleh
    kind regards,

    Jos

  5. #5
    hellolleh is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default

    sorry I don't understand? :confused:

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    My post contains an example. Read it, don't just copy and paste the code.

  7. #7
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    run your code with line

    System.out.println(args.length);

    instead of
    System.out.println(args[0]);

    if you do that, it will show the array length 0(means its empty). but u r trying to print the 0th element(1st element in array) which is not there. thats why its exception.ou
    So, inorder to print the 1st element in array, you need to insert into array. So you do that
    from command line.

    look at josah's suggestion.

Similar Threads

  1. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  2. Replies: 1
    Last Post: 10-26-2009, 04:38 PM
  3. Replies: 3
    Last Post: 01-23-2009, 08:53 AM
  4. error 530 error authentication required
    By rgale in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-12-2008, 04:28 PM

Tags for this Thread

Posting Permissions

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