5

How do I check if the next element in the list is null ?

while(it.hasNext()){

            System.out.println(it.next()+"\n");
        }

this is how I tried, but when the last element is null it prints it as null.

I tried to change it

 while(it.hasNext()){
    if(it.next()==null){
    }else             
        System.out.println(it.next()+"\n");
            }

but this just makes it worst because some of the elements don't even print!

This is my Iteration method/anonymous class

public Iterator<Filmi> iterator3DFilms ()throws FilmiException{
        if(filmList.isEmpty())
            throw new FilmiException("No Films on the list");
        return new Iterator<Filmi>(){
            private int index=0;
            public boolean hasNext(){
                return index <filmList.size();
            }
            public Filmi next(){
                Filmi lb = filmList.get(index++);

                if(lb.is3D()== true)
                    return lb;
                if(hasNext())
                    return next();
                return null;
            }
            public void remove(){}
        };
    }

The null print only happens at the last element Thank you.

t_huang
  • 23
  • 3
Lazlow
  • 63
  • 1
  • 2
  • 6
  • Why there are null values there in the first place? I think the main problem is in the code that populate the iterator. Can't you control that ? – SCouto Jun 17 '16 at 09:36
  • Does the iterator give you the first film in the list? – Keiwan Jun 17 '16 at 09:37
  • in the Filmi next() method it test if the film is 3d and hasNext() if it dosent have any of those it returns a null .thats when the null comes from, but i have to return something . – Lazlow Jun 17 '16 at 09:38
  • @keiwan yes it does – Lazlow Jun 17 '16 at 09:39
  • Every time you call `it.next()` you get the next value. If you want to use this value more than once you have to use a variable and look at this variable. – Peter Lawrey Jun 17 '16 at 09:54

5 Answers5

11

Naturally, code like

if (it.next() == null){
} else {
    System.out.println(it.next()+"\n");
}

will consume every other non-null element, as you are observing. Plus calling it.next() without checking it.hasNext() is a recipe for disaster.

Why not write

Foo/*ToDo - use the correct type here*/ foo = it.next()
if (foo != null){
    /*ToDo*/
}

instead?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
8

No it cannot work this way because if it.next() is not null you call it.next() twice which will make you skip a value that could not even be available.

Use a variable instead as next:

Object o = it.next();
if (o != null) {
   ...
}
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
6

you should use stream instead of iterator.

filmList.stream().filter(film->film!=null).filter(film->film.is3D())

Edit: or, if you'r not in Java 8 :

Predicate<Film> isNotNullAnd3D = new Predicate<Person>() {
        public boolean apply(Film f) {
            return f != null && f.is3D();
        }
    };

Collection2.filter(filmList, isNotNullAnd3D)
sab
  • 4,352
  • 7
  • 36
  • 60
  • 1
    That's only a solution if OP is using Java 8, isn't it? – Fildor Jun 17 '16 at 09:42
  • 2
    Not sure that's necessary a bad thing; Java 8 solutions ought to be considered acceptable on the Java tag. Have an upvote! – Bathsheba Jun 17 '16 at 09:43
  • 1
    I am not arguing that. Just found it worth mentioning because especially beginners may not be aware of that this functionality is Java 8+ only. – Fildor Jun 17 '16 at 09:45
  • yes, but if you don't use Java 8, you can use similar stuff with the lib guava and the class Collections2, method filter – sab Jun 17 '16 at 09:45
  • 1
    But where do you draw the line? Generics are Java 5 onwards, for example. – Bathsheba Jun 17 '16 at 09:46
  • @Bathsheba: I understand that if you still are on Java 7, you can forgot to write-it on your post. But if you'r java 4, he will have precise it on his post, I hope. – sab Jun 17 '16 at 09:50
  • 1
    Might be worth mentioning [Objects::nonNull](https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#nonNull-java.lang.Object-) could be used instead of `film->film!=null` – Hulk Jun 17 '16 at 13:47
1

You never mentioned why you use iterators explicitly in the first place. Why not use implicit iterator notation like this ? :

for (Film film : filmList) {
  if (film != null ){
     ....
  }
}
1

Additionally to what others said: in case you are doing a for-each loop with a primitive type like int

for (int node : input) {
    doSomething(node);
}

you might want to consider using the Wrapper class instead:

for (Integer node : input) {
    if (node == null) throw new IllegalArgumentException();
    doSomething(node);
}
nspo
  • 1,488
  • 16
  • 21