Could it be that your Iterator has a bug?
When iterating over a collection of Cell objects I use a basic while-loop, such as:
while (it.hasNext()) {
Cell cell = (Cell) it.next();
}
The problem occures when I tried to use another ‘next()’ operation, the same object is returned:
while (it.hasNext()) {
Cell cellOne = (Cell) it.next();
Cell cellTwo = (Cell) it.next(); // wrapped in try/catch<br>
}
In the code above (cellOne and cellTwo), both object are the same! But since I know that each rows have multiple cells, they are not suppose to.
The weird part is that in order to make the above work, I have to call ‘hasNext()’ before the call to ‘next()’. The ‘hasNext()’ seems to prepare/fetch the next object (if one is available).
while (it.hasNext()) {
Cell cellOne = (Cell) it.next();<br> if (hasNext()) <br>
Cell cellTwo = (Cell) it.next(); // wrapped in try/catch<br>
}
The above code will work properly! But it’s sad that a call to ‘hasNext()’ must be invoked in order to retreive the second cell which is the one I’m really interested in (every second one).
The code above will work well when I have an even number of cells in each row (in my case two).
But there is a part of my document which only has a single cell.
This is where the real problem starts!
Loop 1...
Line 1: The while block is entered because it does have a cell.
Line 2: The first cell is retreieved.
Line 3: The if block fails because there is only one cell.
Line 4: (not executed because it is part of the if block)
Loop 2...
Line 1: The while is entered because you seem to return true even if there are no cells.
Line 2: Tell you the truth, I don't know why it didn't throw an exception
Line 3: Anyway, it becomes an endless loop...
The only way I could fix this endless loop was by putting in an else clause and a call to ‘hasNext()’ to toggle the boolean return value, such as:
while (it.hasNext()) {
Cell cell = (Cell) it.next();<br>
if (hasNext())
cell = (Cell) it.next();
else
it.hasNext();
}
The above code will work in an odd number of cells per row and eventually loop out of the while block!!
Thanks,
Christian