Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-29-2012, 08:40 PM   PM User | #1
SugarRush
New to the CF scene

 
Join Date: Apr 2012
Location: Ontario, Canada
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
SugarRush is an unknown quantity at this point
Question Exception Handling - Need Help

I'm making a game in Java. I made a new class, and whenever I making an array with it, such as
Code:
private Player players[];
the compiler doesn't generate any errors, but when I run it, there is an ArrayIndexOutOfBoundsException thrown. I couldn't figure out how to solve this problem. Any help?
SugarRush is offline   Reply With Quote
Old 04-30-2012, 03:35 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
That simply says that players will be an array of Player. It doesn't specify how large that array will be, so by default its of size 0. Before trying to read or write to the array you must specify the size of it:
PHP Code:
players = new Player[5]; 
For example. Arrays do not allow you to resize them; this process must be done manually. If you want to be able to dynamically manipulate the array without too much overhead, look at using collections instead. ArrayList, Vector and LinkedLists are all good alternatives depending on what you want to do with them. Each one has a specific purpose for which they shine, but typically speaking the ArrayList and Vector will be the two most interested in. LinkedList is actually wonderful for creating a cyclic queue btw, so if players are round robin in actions, a LinkedList may be a perfect choice (also look at the queue / stacks [actually I'm not 100% sure java has a queue class - there is a priority queue, but that's not quite the same. I'm quite sure it has a stack]).
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
SugarRush (05-02-2012)
Old 04-30-2012, 10:23 PM   PM User | #3
SugarRush
New to the CF scene

 
Join Date: Apr 2012
Location: Ontario, Canada
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
SugarRush is an unknown quantity at this point
I already specified the size to 5(not forgetting the 0 subscript)
Code:
players = new Player[5];
then I did
Code:
for (int i = 0; i < 5; i++)
{
    players[i] = new Player(150, 150, 20, 20);
}
but when I run the game, it still throws the ArrayIndexOutOfBoundsException. Any more information?
SugarRush is offline   Reply With Quote
Old 05-01-2012, 03:32 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This above looks fine to me. Post the code you have in use, you are stepping out of bounds elsewhere.
Fou-Lu is offline   Reply With Quote
Old 05-02-2012, 02:12 AM   PM User | #5
SugarRush
New to the CF scene

 
Join Date: Apr 2012
Location: Ontario, Canada
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
SugarRush is an unknown quantity at this point
Actually, I just figured the solution to the problem. Sorry for wasting your time.
SugarRush is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:00 AM.


Advertisement
Log in to turn off these ads.