| | |
ArrayList null issues...
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Aug 2009
Posts: 12
Reputation:
Solved Threads: 0
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:
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.
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:
Java Syntax (Toggle Plain Text)
for (int i =0; i < nubArr.size(); i++) { if (!nubArr.get(i+1).equals(null)) { if (nubArr.get(i).equals(nubArr.get(i+1))) { if (flush) //if theres a flush { if (cArr.get(i) != soot) //card doesnt match suite { cArr.remove(i); //found a dupe that doesnt match the flush suite nubArr.remove(i); //so remove them both } else if (cArr.get(i+1) != soot) { cArr.remove(i+1); nubArr.remove(i+1); } } else //there isnt a flush. so just erase the first card. { nubArr.remove(i); cArr.remove(i); } } } }
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.
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
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Other Threads in the Java Forum
- Previous Thread: printing a table, please help
- Next Thread: Error - Exit Timer - Please help
| Thread Tools | Search this Thread |
Tag cloud for Java
android api appinventor apple applet application arc arguments array arrays automation binary bluetooth c++ chat class classes client code codesnippet compiler component csv database doctype draw ebook eclipse error event exception fractal freeze game givemetehcodez graphics gui html ide image input integer intellij iphone j2me java java.xls javaprojects jni jpanel julia linux list login loop loops mac map method methods mobile netbeans newbie number online oracle page parameter print problem program programming project recursion reporting rotatetext scanner screen server set size sms socket sort sourcelabs sql string superclass swing system template test testautomation threads time title tree tutorial-sample windows working






