Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Location
    Ontario, Canada
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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]).

  • Users who have thanked Fou-Lu for this post:

    SugarRush (05-02-2012)

  • #3
    New to the CF scene
    Join Date
    Apr 2012
    Location
    Ontario, Canada
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This above looks fine to me. Post the code you have in use, you are stepping out of bounds elsewhere.

  • #5
    New to the CF scene
    Join Date
    Apr 2012
    Location
    Ontario, Canada
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Actually, I just figured the solution to the problem. Sorry for wasting your time.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •