问题:
在使用LoadingCache 缓存对象,对这个对象在build里进行初始化,nullPoint
现象:
List streamingPoolKeyList = streamingPoolKeys.getData();
streamingPoolContainerCahche.put(container, streamingPoolKeyList); //nullPonit
日志:
Caused by: java.lang.NullPointerException: null at [com.google.common.base.Preconditions.checkNotNull(Preconditions.java:770](http://com.google.common.base.preconditions.checknotnull%28preconditions.java:770/)) at [com.google.common.cache.LocalCache.put(LocalCache.java:4363](http://com.google.common.cache.localcache.put%28localcache.java:4363/)) at [com.google.common.cache.LocalCache$LocalManualCache.put(LocalCache.java:5077](http://com.google.common.cache.localcache%24localmanualcache.put%28localcache.java:5077/))
原因:
streamingPoolContainerCahche.put(container, streamingPoolKeyList);
接收到的value:streamingPoolKeyList 为null
loadingCache put元素时 key,value 不能为null
google使用 checkNotNull 方法检查
public V put(K key, V value) {
checkNotNull(key);
checkNotNull(value);
int hash = hash(key);
return segmentFor(hash).put(key, hash, value, false);
}
public static T checkNotNull(T reference) {
if (reference == null) {
throw new NullPointerException();
}
return reference;
}
如果key或value为null,会有nullPoint错误
总结:
loadingCache put 方法的key,value不能为null