File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why doesn't this iterator return an object?

 
James Gibbs
Greenhorn
Posts: 29
Android Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to iterate through a list and add all matching nodes to another list. However in the specific line affiliateResults.add(iterator.next()), iterator.next doesn't return an object, as when I later inspect affiliateResults it is empty. Any ideas where I'm going wrong?

 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're potentially calling iterator.next() twice for each call to iterator.hasNext() (in lines 3 and 8) - you should call next() only once for each call to hasNext().
 
James Gibbs
Greenhorn
Posts: 29
Android Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah thanks I see what you mean. Now it changes my question how I convert a JsonNode to a Row? maybe I'd be better off with a for loop
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of line 5, write something like:

Row row = iterator.next();

JsonNode doc = row.getValueAsNode();
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Gibbs wrote:ah thanks I see what you mean. Now it changes my question how I convert a JsonNode to a Row? maybe I'd be better off with a for loop

I'm probably misunderstanding what you're trying to do here...but iterator.next() is returning a Row. If you want a Row instead of a JsonNode, don't call getValueAsNode() on it.


By the way, there's a little trick you can use instead of this pattern:

You can replace that with:
but even more concise is:
 
James Gibbs
Greenhorn
Posts: 29
Android Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf and Matt, with your help I saw the errors of my ways and got it working.

Matt, thank you so much for your advice about the inner if conditional, when I was writing it I was thinking "there has to be better way", did consider && but had completely forgotten that it short circuits so thought if I used it it might blow up if x.equals("yes") was run before the null check.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic