• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception in thread  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've written this code so far and only 1 error but it's driving me nuts, maybe lack of sleep or something far worse, who knows. If any one can help, thank you in advance.


This is the error I'm getting any help would be appreciated in advance, thank you!

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at QuickSort.main(QuickSort.java:77)
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you running it? I ran it using this command


and this was my output:

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you called this program without a parameter?
 
Sheriff
Posts: 57845
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Go back to your Exception error message; it has a line no on ?77. Go to your line 77 and there you will find an array used. If the index is 0 it means you don't have a 1st member.
There are several possibilities for this error:-
  • 1: You have an array declared but have never created it with a statement like: fooArray = new foo[20];
  • 2: You have an array which you have never inserted any members into.
  • Are you using the args[] array without passing anything from the command line? That is a potent source of unexpected ArrayIndexEtcExceptions.

    CR
    [ February 11, 2007: Message edited by: Campbell Ritchie ]
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think the error comes when there is no argument passed with the run command.
    This can be handled this way:

    Replace int N = Integer.parseInt(args[0]);
    with the following code:

    int N = 0;
    if(args.length > 0)
    N = Integer.parseInt(args[0]);

    OR

    int N = 0;
    try
    {
    N = Integer.parseInt(args[0]);
    }
    catch (ArrayIndexOutOfBoundsException aiob)
    {
    System.out.println("No value recieved!!!");
    }
     
    author
    Sheriff
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Originally posted by Campbell Ritchie:

    There are several possibilities for this error:-

  • 1: You have an array declared but have never created it with a statement like: fooArray = new foo[20];
  • 2: You have an array which you have never inserted any members into.

  • Not quite - the first would cause a NullPointerException when trying to access the array, the latter doesn't make sense to me because you don't really *insert* members to arrays.

    Really the only way this error can happen is when the array has a length of zero.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!