Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Exception in thread

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps you called this program without a parameter?
 
Marshal
Posts: 74085
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Posts: 14112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic