Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8015008

Primitive iterator over empty sequence, null consumer: forEachRemaining methods do not throw NPE

XMLWordPrintable

      Following code executes silently on b88 - NullPointerException is not thrown:

      import java.util.PrimitiveIterator;
      import java.util.function.Consumer;
      import java.util.function.IntConsumer;

      public class PrimitiveIteratorForEachRemaining implements PrimitiveIterator.OfInt {

          public static void main(String argv[]) {
              new PrimitiveIteratorForEachRemaining().forEachRemaining((IntConsumer) null);
              new PrimitiveIteratorForEachRemaining().forEachRemaining((Consumer<Integer>) null);
          }
          @Override
          public int nextInt() {
              return 0;
          }

          @Override
          public boolean hasNext() {
              return false;
          }

      }

      Similar thing does happen for both PrimitiveIterator.OfLong and PrimitiveIterator.OfDouble as well.

      According to forEachRemaining(IntConsumer) spec NPE should be thrown.

      I wonder if spec should specify that NPE is thrown when forEachRemaining(Consumer) is passed a null as a consumer?

      The code is attached for your convenience.

            psandoz Paul Sandoz
            grakov Georgiy Rakov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: