Results 1 to 6 of 6
  1. #1
    sudhap is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default exception in thread main java.lang.arrayindexoutofboundsexception 0

    Hi,

    i am getting the" exception in thread main java.lang.arrayindexoutofboundsexception 0 " error when i execute the program.

    Can some one please help me to resolve it?

    here is my program.

    class InvertTriangle{

    public static void main(String args[]){

    int num = Integer.parseInt(args[0]);

    while(num > 0){

    for(int j=1;j<=num;j++){

    System.out.print(" "+num+" ");

    }

    System.out.print("\n");

    num--;

    }

    }

    }

  2. #2
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: exception in thread main java.lang.arrayindexoutofboundsexception 0

    You need to look at the syntax of your code. You have some problems, pretty basic, but totally incorrect. Start here:The Java™ Tutorials

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

    Default Re: exception in thread main java.lang.arrayindexoutofboundsexception 0

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: exception in thread main java.lang.arrayindexoutofboundsexception 0

    You need to check if the array: args is empty before trying to get at its contents.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    sudhap is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: exception in thread main java.lang.arrayindexoutofboundsexception 0

    I am sorry i did not understand what you are telling as i am new to java.
    can you please correct my code and tell where i did a mistake?

    Thanks,

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: exception in thread main java.lang.arrayindexoutofboundsexception 0

    Did Find the code where the exception occurs? Please copy the statement here if you have questions about it.
    What array is used in that statement?
    Test the length of the array using the .length attribute to see if the array is empty: length > 0.
    If the length is 0 do not try to get any elements from the array. Use an if statement to test and skip using the array if it is empty.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: 02-04-2011, 08:31 PM
  2. Replies: 3
    Last Post: 02-03-2011, 04:49 PM
  3. Replies: 4
    Last Post: 02-02-2011, 04:32 AM
  4. Replies: 5
    Last Post: 04-05-2010, 02:28 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
  •