vigna / fastutil

fastutil extends the Java™ Collections Framework by providing type-specific maps, sets, lists and queues.
Apache License 2.0
1.74k stars 194 forks source link

Iterate entries in map always throws NPE if map is empty. #278

Closed GenCloud closed 1 year ago

GenCloud commented 1 year ago

Good day, why is it that when iterating a map, an NPE error always occurs because the internal collection has not been initialized.

MapIterator#nextEntry:L657 wrapped.getInt(-pos - 1);

wrapped (IntArrayList) is always null, because not initialized. java.lang.NullPointerException: Cannot invoke "it.unimi.dsi.fastutil.ints.IntArrayList.getInt(int)" because "this.wrapped" is null at it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap$MapIterator.nextEntry(Int2ObjectOpenHashMap.java:657) at it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap$EntryIterator.next(Int2ObjectOpenHashMap.java:750) at it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap$EntryIterator.next(Int2ObjectOpenHashMap.java:745)

example to reproduce: Int2ObjectMap<Object> objects = Int2ObjectMaps.synchronize(new Int2ObjectOpenHashMap<>(1)); final ObjectSet<Int2ObjectMap.Entry<Object>> entries = objects.int2ObjectEntrySet(); for (Int2ObjectMap.Entry<Object> entry : entries) { // <-- nextEntry NPE // some logic } lib version: 8.5.8

vigna commented 1 year ago

I tried the code above and it does not throw anything. Please write a unit test.