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

Collectors.toMap fails on null values

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.8.0_72"
      Java(TM) SE Runtime Environment (build 1.8.0_72-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 25.72-b15, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.3.9600]

      A DESCRIPTION OF THE PROBLEM :
      When used on null values, Collectors.toMap fails with a NullPointerException, see source below

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Look at the source code below: the first collect operation works as expected, the second crashes.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Both collect operations should work
      ACTUAL -
      Exception in thread "main" java.lang.NullPointerException
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
      at java.util.concurrent.ForkJoinTask.getThrowableException(ForkJoinTask.java:598)
      at java.util.concurrent.ForkJoinTask.reportException(ForkJoinTask.java:677)
      at java.util.concurrent.ForkJoinTask.invoke(ForkJoinTask.java:735)
      at java.util.stream.ReduceOps$ReduceOp.evaluateParallel(ReduceOps.java:714)
      at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:233)
      at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
      at stuff.TestMapCollectorProblem.main(TestMapCollectorProblem.java:15)
      Caused by: java.lang.NullPointerException
      at java.util.HashMap.merge(HashMap.java:1216)
      at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
      at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
      at java.util.HashMap$EntrySpliterator.forEachRemaining(HashMap.java:1683)
      at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
      at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
      at java.util.stream.ReduceOps$ReduceTask.doLeaf(ReduceOps.java:747)
      at java.util.stream.ReduceOps$ReduceTask.doLeaf(ReduceOps.java:721)
      at java.util.stream.AbstractTask.compute(AbstractTask.java:316)
      at java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:731)
      at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
      at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
      at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
      at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package stuff;

      import java.util.HashMap;
      import java.util.stream.Collectors;

      public class TestMapCollectorProblem {
          public static void main(String[] args) {
              HashMap<String, String> m = new HashMap<>();
              m.put("A", "a");
              m.put("B", "b");
              m.put("C", "c");
              m.entrySet().parallelStream().collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));

              m.put("D", null);
              m.entrySet().parallelStream().collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
          }
      }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            2 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: