• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why doesn't this iterator return an object?

 
Greenhorn
Posts: 29
Android Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of line 5, write something like:

Row row = iterator.next();

JsonNode doc = row.getValueAsNode();
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic