Click here to Skip to main content
13,052,956 members (61,690 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Alright guys firstly I'm working with XNA 4, secondly I'm using ArrayLists.

Now the problem is, I have no idea why these exceptions occur however they DO occur and apparently incorrectly.

Here's the code where it happens:

if (bullets.Count - 1 >= i)

As you can see I even added the if statement so it wouldn't execute those lines unless 'i' is indeed within the range of the ArrayList. When the exception is thrown, my 'i' value is always contained within the range, and I have no idea what is going on.

Is there anything I'm overlooking?
Posted 23-Apr-11 7:30am
Updated 23-Apr-11 7:37am
EdMan196 23-Apr-11 14:48pm
Are you sure 'i' is within the range of both 'bullets' and 'bulDirVal'? Your if statement should look something like: if(i < bullets.Count && i < bulDirVal.Count) ... (FYI using '< X.Count' is better than 'X.Count - 1 >= ' as it is both clearer and computationally more efficient, but that is another topic :P)

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

As Edman pointed out, you only check one collection, you also need to check the other one.

Another possibility is that you have multiple threads that may be accessing the same collection. If so, you need to add some thread-sync in there.
Raztor0 23-Apr-11 16:57pm
Both the ArrayLists are always the same size and get expanded/reduced at the same time as well, and oh yes multiple threads definately seems like it could be the problem here :o thanks a lot both of you :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 23 Apr 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100