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
Tricky NullPointerException in IntObjectMap.forEachKeyValue #1418
Comments
While the above is true, I don't believe we support map modifications within Perhaps that needs to be better documented. Additionally, we could add |
It is absolutely safe (even if by accident). Only this case with key==0 is a problem. I didn't want to call
Real code (migrated from JDK Map) looks like this: void verify (@Nonnull MutableIntObjectMap<byte[]> m){
m.forEachKeyValue((ie,b)->{
int iel = b.length;//actual ie len
Integer ielMin = IE_LEN_MIN.get(ie);
if (ielMin != null && iel < ielMin) {
m.removeKey(ie);
}
Integer ielMax = IE_LEN_MAX.get(ie);
if (ielMax != null && iel > ielMax) {
m.removeKey(ie);
}
});
} I use EC only few hours. Probably there is a more idiomatic way to do this. PS: BUT bug with NPE is easy to fix. So, why keep it lying there ;-) Other primitive key Map implementations have it probably too. |
The above code fits well within a We have two choices here:
Relying on an accident, instead of an intent, is neither good for your code nor for the library. |
I would choose both :-)
|
I've just checked: this pattern is everywhere if (this.sentinelValues != null)
{
if (this.sentinelValues.containsZeroKey)
{
procedure.value(EMPTY_KEY);
}
if (this.sentinelValues.containsOneKey)// a place to hit your head
{
procedure.value(REMOVED_KEY);
}
} in forEachKey, forEachValue, etc... And it means: it is really hard to remove from primitive Maps... |
If you remove key 0 (int; aka EMPTY_KEY) in
forEachKeyValue
- you will enjoyNullPointerException
Problem is here:
org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap#forEachKeyValue
if (this.sentinelValues.containsOneKey)
after removing key 0 aka EMPTY_KEY - sentinelValues can be null
The problem is easy to fix:
The text was updated successfully, but these errors were encountered: