Computer Science Canada java.lang.ArrayIndexOutOfBoundsException error |
Author: | mapleleafs [ Sat Jul 24, 2004 2:03 pm ] | ||
Post subject: | java.lang.ArrayIndexOutOfBoundsException error | ||
this is my program(actually from a book)
and i get this error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at truncator.main(truncator.java:6) i'm a newbie at java so help is much appreciated |
Author: | wtd [ Sat Jul 24, 2004 6:12 pm ] | ||
Post subject: | |||
That means that the array isn't long enough for the index you're trying to access. in Java (as in many other programing languages) zero is the first index of an array. If the array is empty, though, then attempting to access the zero-th element will result in an ArrayIndexOutOfBoundsException. Try:
|
Author: | rizzix [ Sun Jul 25, 2004 1:24 am ] |
Post subject: | |
actually ur better off not using a try block for this kinda error handling.. cuz is best u handle it in ur code. use a if statement and check the length of the args array. if it is greater than zero.. u can do whatever u what else u do something else. now if there is no something else whatsoever. then i suggest using the try-block. |
Author: | guruguru [ Sun Jul 25, 2004 12:34 pm ] | ||
Post subject: | |||
I bellieve that should work ![]() |
Author: | wtd [ Sun Jul 25, 2004 2:52 pm ] |
Post subject: | |
Except that there's no reason x shouldn't be local to the main method. |
Author: | guruguru [ Sun Jul 25, 2004 8:02 pm ] | ||
Post subject: | |||
Wow... umm ok...
![]() |
Author: | wtd [ Mon Jul 26, 2004 2:47 am ] | ||
Post subject: | |||
Perhape even better...
|
Author: | wtd [ Tue Jul 27, 2004 4:58 pm ] |
Post subject: | |
As a matter of convention, all class names in Java should begin with a capital letter. |