Java Problem


Mosch

Reeks of fish
Joined
Feb 3, 2004
Messages
978
Age
40
Location
Germany
Website
Visit site
So we have quite some coders here, maybe one of you can help me with my problem....

Here are my Variables:
CODE
int a[] = new int[10];
String s[] = new String[10];
Random die = new Random();


And here are two "fors":

CODE
for ( int index = 1; index <= 10; index++ )
{
a[index] = die.nextInt(6);
a[index]++;
s[index] = String.valueOf(a[index]);
}

for ( int index = 1; index <= 10; index++ )
{
aGUI.klChoice.add(s[index]);
aGUI.inChoice.add(s[index]);
aGUI.chChoice.add(s[index]);
aGUI.diChoice.add(s[index]);
aGUI.skChoice.add(s[index]);
aGUI.koChoice.add(s[index]);
aGUI.ffChoice.add(s[index]);
aGUI.geChoice.add(s[index]);
aGUI.kkChoice.add(s[index]);
aGUI.wnChoice.add(s[index]);
}


They both seem to produce an infinite loop - or at least somehow make my program stop. I tried to catch a NullPointerException, but it came up with nothing. Now I have not programmed in Java for two years, but I really don't see the problem.
 
I'm not sure since I also haven't done Java for a while... but those for conditions look suspect... I think:

for ( int index = 1; index <= 10; index++ )

needs to be

for ( int index = 1; index < 10; index++ )

It all depends on how Java does arrays...
 
YES! I'm an idiot, thanks for pointing it out :D I saved a[0] and s[0] for control, but did not remember to include them in the actual array length.

...can't believe it took me more than a day to figure that out...

EDIT: Still, why didn't I get a NullPointerException? Curiouser and Curiouser....
 
I don't think an index out of bounds gives a NullPointerException, rather a IndexOutOfBounds or something like that. Also, instead of 10, wouldn't it be slightly cleaner to use a.length and s.length, respectively?
 
Alex. said:
I don't think an index out of bounds gives a NullPointerException, rather a IndexOutOfBounds or something like that.
That might be an explanation.

Alex. said:
Also, instead of 10, wouldn't it be slightly cleaner to use a.length and s.length, respectively?
Kinda, but as they are constants, meh, who cares.
 
Last edited by a moderator:
Yeah, to sum it up, don't look for Nullpointer but an ArrayOutOfBounds, don't use constant lenghts and remember that Java Arrays start with 0. Also, if you are using the GP2x DevKit you can redirect the console output to a file.
 
I know that Java Arrays start with 0. I use a[0] and s[0] to store the character's class, [1] to [10] to store attributes. What I did not think of was that my "new int[10]" creates and Array with 10 usable elements instead of an array that goes up to [10]. We got that cleared up nicely though ;)

And I'm not programming for the GP2x by the way (don't have one) - I didn't even know there's a java interpreter for it.
 
Mosch said:
I know that Java Arrays start with 0. I use a[0] and s[0] to store the character's class, [1] to [10] to store attributes. What I did not think of was that my "new int[10]" creates and Array with 10 usable elements instead of an array that goes up to [10]. We got that cleared up nicely though ;)


Yeah, I just wanted to sum it up for the tl;dnr people.

QUOTE

And I'm not programming for the GP2x by the way (don't have one) - I didn't even know there's a java interpreter for it.


Yeah, there is, Java 1.5 actually without AWT and Swing but SDL support. Runs pretty well although some trolls will always complain.
 
Last edited by a moderator:
Back
Top