File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception in thread

 
Michael Statham
Greenhorn
Posts: 1
  • 0
  • 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)
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • 0
  • 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:

 
Anton Uwe
Ranch Hand
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you called this program without a parameter?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47279
52
  • 0
  • 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 ]
     
    kiranmayi srivastava
    Greenhorn
    Posts: 1
    • 0
    • 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!!!");
    }
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • 0
    • 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.
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic