简体   繁体   English

具有多个空值的多图上的番石榴空指针

[英]Guava null pointer on multimap with multiple null values

I'm using guava multimap (TreeMultiMap). 我正在使用番石榴多图(TreeMultiMap)。 I'm doing some unit tests to ensure it's ok but when I do the following test I get a null pointer exception from guava (I've cobbled together some code to recreate it). 我正在做一些单元测试以确保它没问题,但是当我进行以下测试时,我从番石榴得到了空指针异常(我已经整理了一些代码来重新创建它)。 In the below just assume Location/Interconnect are simple classes. 在下面,仅假设位置/互连是简单的类。 The natural order is by the id for the Location, ie 50, 51 in the code below. 自然顺序是由位置的ID决定的,即以下代码中的50、51。

Multimap<Location, Interconnect> interconnects;
interconnects = TreeMultimap.create();

Location l1 = new Location(50, "Test Location 1");
assertTrue(interconnects.put(l1, null));

Location l2 = new Location(51, "Test Location 2");
assertTrue(interconnects.put(l2, null));

//expect false but get null exception
assertFalse(locs.addLocation(l9));

I would have thought the last line being added would have returned simply false but it causes an exception. 我本以为要添加的最后一行将仅返回false,但这会导致异常。

If I change the null with a value they the second l9 returns false as I expected. 如果我将null更改为值,则第二个l9如我所料返回false。

What's going wrong? 怎么了 All I could think of was Maps allow only one null key but this isn't a standard map and it isn't a key, it's the value. 我只想到Maps只允许一个null键,但这不是标准的map,也不是key,而是值。

The reason for testing this is I wish to allow adding of a Location without any values (as they may not be known) but then allow them to be added when they are known. 测试它的原因是,我希望允许添加不带任何值的Location(因为它们可能未知),然后允许在已知时添加它们。 I appreciate I can put in checks for duplicates with nulls but I want to know why it's failing in this scenario. 我很感谢我可以检查是否有空值重复项,但我想知道为什么在这种情况下失败了。

You're using a TreeMultimap , which means the values have to support the compareTo method. 您正在使用TreeMultimap ,这意味着这些值必须支持compareTo方法。 Trying to use compareTo on null values will throw a NullPointerException . 尝试在null值上使用compareTo将抛出NullPointerException

Except that when you add one value, there's no other value to compare it to, so compareTo isn't called until the second value, at which point everything falls over. 除了添加一个值时,没有其他值可与之比较,因此直到第二个值都不会调用compareTo ,此时一切都完结了。 TreeSet has similar issues, though they may have been fixed in more recent versions of Java. TreeSet也有类似的问题,尽管它们可能已在Java的较新版本中修复。

Workarounds include using Ordering.natural().nullsFirst() or another null-accepting Comparator in the TreeMultimap , or using another Multimap implementation. 变通方法包括使用Ordering.natural().nullsFirst()或另一种空接受ComparatorTreeMultimap ,或使用另一种Multimap实现。

FWIW, Guava's Multimap is very much not designed to be used in the way you're using it, with a "Location without any values." FWIW,番石榴的Multimap非常不适宜在你使用它的方式使用,具有“位置没有任何价值。” You'd probably be better off falling back to the traditional Map<Key, Set<Value>> . 您最好还是回到传统的Map<Key, Set<Value>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM