哈希表获取现有密钥为空 [英] Hashtable getting null for existing key
本文介绍了哈希表获取现有密钥为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Vaadin的调试器显示以下状态:
The debugger shows this state:
那怎么可能? object2
如何成为null
?
How is that even possible? How can object2
be null
?
这是导致NPE(类EstablishRelationWindow
)的实际代码:
This is my actual code which causes the NPE (class EstablishRelationWindow
):
childrenContainer = new BeanItemContainer<>(PlaylistDTO.class);
childrenContainerHierarchy = new ContainerHierarchicalWrapper(childrenContainer);
buildChildrenTree(somePlaylistDTO);
private void buildChildrenTree(PlaylistDTO current) {
childrenContainer.addBean(current);
if(current.getChildren().isEmpty()) {
childrenContainerHierarchy.setChildrenAllowed(current, false);
} else {
current.getChildren().forEach(child -> {
buildChildrenTree(child);
childrenContainerHierarchy.setParent(child, current); //line 104
});
}
}
相关输出:
java.lang.NullPointerException: null
at java.util.Collections.sort(Collections.java:175) ~[na:1.8.0_60]
at com.vaadin.data.util.ContainerHierarchicalWrapper.updateHierarchicalWrapper(ContainerHierarchicalWrapper.java:191) ~[vaadin-server-7.6.8.jar:7.6.8]
at com.vaadin.data.util.ContainerHierarchicalWrapper$PiggybackListener.containerItemSetChange(ContainerHierarchicalWrapper.java:838) ~[vaadin-server-7.6.8.jar:7.6.8]
at com.vaadin.data.util.AbstractContainer.fireItemSetChange(AbstractContainer.java:242) ~[vaadin-server-7.6.8.jar:7.6.8]
at com.vaadin.data.util.AbstractContainer.fireItemSetChange(AbstractContainer.java:228) ~[vaadin-server-7.6.8.jar:7.6.8]
at com.vaadin.data.util.ContainerHierarchicalWrapper.fireItemSetChangeIfAbstractContainer(ContainerHierarchicalWrapper.java:506) ~[vaadin-server-7.6.8.jar:7.6.8]
at com.vaadin.data.util.ContainerHierarchicalWrapper.setParent(ContainerHierarchicalWrapper.java:495) ~[vaadin-server-7.6.8.jar:7.6.8]
at com.xinra.listaide.frontend.EstablishRelationWindow.lambda$3(EstablishRelationWindow.java:104) ~[classes/:na]
at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_60]
at com.xinra.listaide.frontend.EstablishRelationWindow.buildChildrenTree(EstablishRelationWindow.java:102) ~[classes/:na]
创建PlaylistDTO对象的代码:
Code that creates PlaylistDTO objects:
public class DynamicProxyDTOFactory implements DTOFactory {
@SuppressWarnings("unchecked")
@Override
public <T extends DTO> T createDTO(Class<T> type) {
return (T) Proxy.newProxyInstance(
type.getClassLoader(),
new Class<?>[] { type },
new DynamicProxy());
}
private static class DynamicProxy implements InvocationHandler {
private Map<String, Object> properties = new HashMap<>();
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if(method.getName().startsWith("set")) {
properties.put(method.getName().substring(3), args[0]);
return null;
} else if(method.getName().startsWith("get")) {
return properties.get(method.getName().substring(3));
} else if(method.getName().startsWith("is")) {
return properties.get(method.getName().substring(2));
} else {
return method.invoke(this, args); //for equals, hashCode etc.
}
}
}
}
推荐答案
我整理了一个SSCCE并进一步调查了该问题.原来,这实际上根本不起作用:
I put together an SSCCE and investigated the issue further. Turns out, this actually doesn't work at all:
} else {
return method.invoke(this, args); //for equals, hashCode etc.
}
相反,我扩展了DynamicProxy
,如此示例中所示:
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if(method.getName().startsWith("set")) {
properties.put(method.getName().substring(3), args[0]);
return null;
} else if (method.getName().startsWith("get")) {
return properties.get(method.getName().substring(3));
} else if (method.getName().startsWith("is")) {
return properties.get(method.getName().substring(2));
} else if (method.getName().equals("equals")) {
return proxy == args[0];
} else if (method.getName().equals("hashCode")) {
return System.identityHashCode(proxy);
} else if (method.getName().equals("toString")) {
return proxy.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(proxy))
+ " with InvocationHandler " + this;
} else {
throw new IllegalStateException(String.valueOf(method));
}
}
这样hashCode
尤其是equals
可以按预期方式工作,并且NPE
不会发生.
This way hashCode
and especially equals
work as intended and the NPE
does not occur.
这篇关于哈希表获取现有密钥为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文