ArrayList null issues...

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2009
Posts: 12
Reputation: ryno365 is an unknown quantity at this point 
Solved Threads: 0
ryno365 ryno365 is offline Offline
Newbie Poster

ArrayList null issues...

 
0
  #1
Oct 30th, 2009
Hey DW. Working on a nice poker application and need some help. Right now I'm trying to go through two arraylists, one of chars and one of sorted ints (representing the suites, and numbers of the cards) and delete duplicating numbs (and the char/suite counterpart in the other array).

In this function, I have already made code that detects if a flush exists (and if it does, the char/suite is stored in soot). Here is the rest of the code:

  1. for (int i =0; i < nubArr.size(); i++)
  2. {
  3.  
  4. if (!nubArr.get(i+1).equals(null))
  5. {
  6. if (nubArr.get(i).equals(nubArr.get(i+1)))
  7. {
  8. if (flush) //if theres a flush
  9. {
  10. if (cArr.get(i) != soot) //card doesnt match suite
  11. {
  12. cArr.remove(i); //found a dupe that doesnt match the flush suite
  13. nubArr.remove(i); //so remove them both
  14. }
  15. else if (cArr.get(i+1) != soot)
  16. {
  17. cArr.remove(i+1);
  18. nubArr.remove(i+1);
  19. }
  20. }
  21. else //there isnt a flush. so just erase the first card.
  22. {
  23. nubArr.remove(i);
  24. cArr.remove(i);
  25. }
  26. }
  27. }
  28. }

And here's my problem...
Notice the runtime error on line 4 where an element that doesnt exist in the AL is accessed to check to see if its null. This is where my problem is. I'm trying to check if I've reached the end of the array list. But I can't seem to find a good way of doing. I'm also trying to avoid iterators. Any help is appreciated.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,238
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 490
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer
 
0
  #2
Oct 30th, 2009
As you already found out you can not check for something that is not there, something you did not declare.
You may consider simple work around like this
if (i+1 != nubArr.size()) taking in consideration that i+1 can only be smaller than or equal to array list size, as for loop will not let you go over array list size
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 12
Reputation: ryno365 is an unknown quantity at this point 
Solved Threads: 0
ryno365 ryno365 is offline Offline
Newbie Poster
 
0
  #3
Oct 30th, 2009
thanks dude. appreciate it.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC