Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hashCode of a ju.MapWrapper containing a null value throws a NullPointerException #8504

Closed
scabug opened this issue Apr 15, 2014 · 2 comments
Assignees
Labels
Milestone

Comments

@scabug
Copy link

scabug commented Apr 15, 2014

Calling hashCode on a java map wrapper fails, because hashCode in Wrappers.scala (line 190) calls v.hashCode without checking for null. This seems unexpected since calling hashCode on a scala map works fine even if it contains a null value (and I believe the same goes for java).

It also breaks some iteration constructs; for example, calling .zipWithIndex on a wrapped map containing a wrapped map that has a null value throws a NullPointerException

Here's a stack trace/how to reproduce:

Simple case:

❯ scala                                                                                                                                                              1Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_40).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import scala.collection.JavaConversions._
import scala.collection.JavaConversions._

scala> val scalaMap = Map(1 -> null)
scalaMap: scala.collection.immutable.Map[Int,Null] = Map(1 -> null)

scala> val javaMap = mapAsJavaMap(scalaMap)
javaMap: java.util.Map[Int,Null] = {1=null}

scala> scalaMap.hashCode
res0: Int = 1600778741

scala> javaMap.hashCode
java.lang.NullPointerException
	at scala.collection.convert.Wrappers$MapWrapper$$anon$1$$anon$5$$anon$6.hashCode(Wrappers.scala:190)
	at java.util.AbstractMap.hashCode(AbstractMap.java:494)
	at .<init>(<console>:13)
	at .<clinit>(<console>)
	at .<init>(<console>:7)
	at .<clinit>(<console>)
	at $print(<console>)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734)
	at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983)
	at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573)
	at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604)
	at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568)
	at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:745)
	at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:790)
	at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:702)
	at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:566)
	at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:573)
	at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:576)
	at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:867)
	at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
	at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
	at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
	at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822)
	at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:83)
	at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)
	at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:105)
	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

Case where zipWithIndex breaks:

scala> val javaMap2 = mapAsJavaMap(Map(1 -> 2, 3 -> mapAsJavaMap(Map(4 -> null))))
javaMap2: java.util.Map[Int,Any] = {1=2, 3={4=null}}

scala> javaMap2.zipWithIndex
java.lang.NullPointerException
	at scala.collection.convert.Wrappers$MapWrapper$$anon$1$$anon$5$$anon$6.hashCode(Wrappers.scala:190)
	at java.util.AbstractMap.hashCode(AbstractMap.java:494)
	at scala.runtime.ScalaRunTime$.hash(ScalaRunTime.scala:214)
	at scala.util.hashing.MurmurHash3.productHash(MurmurHash3.scala:63)
	at scala.util.hashing.MurmurHash3$.productHash(MurmurHash3.scala:210)
	at scala.runtime.ScalaRunTime$._hashCode(ScalaRunTime.scala:176)
	at scala.Tuple2.hashCode(Tuple2.scala:19)
	at scala.runtime.ScalaRunTime$.hash(ScalaRunTime.scala:214)
	at scala.collection.mutable.HashTable$HashUtils$class.elemHashCode(HashTable.scala:398)
	at scala.collection.mutable.HashMap.elemHashCode(HashMap.scala:39)
	at scala.collection.mutable.HashTable$class.findOrAddEntry(HashTable.scala:161)
	at scala.collection.mutable.HashMap.findOrAddEntry(HashMap.scala:39)
	at scala.collection.mutable.HashMap.$plus$eq(HashMap.scala:89)
	at scala.collection.mutable.HashMap.$plus$eq(HashMap.scala:39)
	at scala.collection.IterableLike$$anonfun$zipWithIndex$1.apply(IterableLike.scala:268)
	at scala.collection.IterableLike$$anonfun$zipWithIndex$1.apply(IterableLike.scala:267)
	at scala.collection.Iterator$class.foreach(Iterator.scala:727)
	at scala.collection.AbstractIterator.foreach(Iterator.scala:1157)
	at scala.collection.IterableLike$class.foreach(IterableLike.scala:72)
	at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
	at scala.collection.IterableLike$class.zipWithIndex(IterableLike.scala:267)
	at scala.collection.AbstractIterable.zipWithIndex(Iterable.scala:54)
	at .<init>(<console>:12)
	at .<clinit>(<console>)
	at .<init>(<console>:7)
	at .<clinit>(<console>)
	at $print(<console>)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734)
	at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983)
	at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573)
	at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604)
	at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568)
	at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:745)
	at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:790)
	at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:702)
	at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:566)
	at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:573)
	at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:576)
	at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:867)
	at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
	at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
	at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
	at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822)
	at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:83)
	at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)
	at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:105)
	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
@scabug
Copy link
Author

scabug commented Apr 15, 2014

Imported From: https://issues.scala-lang.org/browse/SI-8504?orig=1
Reporter: Ilya Brin (ibrin)
Affected Versions: 2.10.2

@scabug
Copy link
Author

scabug commented Apr 29, 2014

@gourlaysama said:
PR: scala/scala#3704

@scabug scabug closed this as completed May 8, 2014
@scabug scabug added this to the 2.11.1 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants