Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException problem on mac

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Smile Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException problem on mac

    No matter whatever other code i write on my mac apart from the very popular Hello World, no other one seems to work on my mac.
     
    public class Distance1 {
     
    	public static void main(String[] args){
    		int a = Integer.parseInt( args[0] );
    		int b = Integer.parseInt( args[1] );
     
    		int dist = Math.max(a,b) - Math.min(a,b);
    		System.out.println( dist );
     
    	}
     
    }

    The error i get is: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Distance1.main(distance1.java:4)

    Since i'm just a beginner, i dont know what it means. Can any1 help me with it?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException problem on mac

    DOes the args array contain any elements? Use the .length attrubute to test if there are enough elements in the array before trying to access any of its elements.

    If the array is empty there is no first element (index=0) and you'll get: ArrayIndexOutOfBoundsException: 0

    The args array is filled with what is on the command line when the java command starts the class. For example:
    java TheClass thisIsArg1 thisIsArg2

    when the main() method executes, args[0] would have the value: "thisIsArg1", and args[1] would be "thisIsArg2"
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException problem on mac

    i think you are not provide the command line arguments. Run the program with command line arguments like

    > java Distance1 4 5

  4. The Following User Says Thank You to basha2013 For This Useful Post:

    hemla (January 23rd, 2013)

  5. #4
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException problem on mac

    Thanks to all of you who answered but now I get a different error:

    Distance1.java:7: cannot find symbol
    symbol : variable max
    location: class java.lang.Math
    System.out.println(Math.max - Math.min);
    ^
    Distance1.java:7: cannot find symbol
    symbol : variable min
    location: class java.lang.Math
    System.out.println(Math.max - Math.min);
    ^
    2 errors
    And now i'm confused :S

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException problem on mac

    Where did you see that the max and min variables are defined in the Math class?
    The compiler can not find their definitions in the Math class.

    Read the API doc for the Math class to see what variables and methods are in the class and how to use them.

    See post#1 for some usages of Math methods.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    hemla (January 23rd, 2013)

  8. #6
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException problem on mac

    Problem solved!

Similar Threads

  1. Replies: 5
    Last Post: December 9th, 2012, 02:25 PM
  2. Exception in thread "main" java.lang.NullPointerException problem.......
    By Adam802 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 20th, 2012, 02:23 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  5. Replies: 2
    Last Post: March 26th, 2010, 11:22 AM