【问题标题】:Hashtable getting null for existing key哈希表为现有键获取空值
【发布时间】:2016-09-12 10:45:36
【问题描述】:

我在这段来自 Vaadin 的 ContainerHierarchicalWrapper 的代码中有一个非常奇怪的错误:

for (Object object : children.keySet()) {
    LinkedList<Object> object2 = children.get(object);
}

调试器显示此状态:

这怎么可能? object2怎么可能是null

这是导致 NPE 的实际代码(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 对象的代码:

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.
            }
        }   
    }

}

【问题讨论】:

  • 也许您的密钥没有实现正确的hashCodeequals 方法?您可以尝试迭代地图的 entrySet。 for (Map.Entry&lt;Object, Object&gt; entry : children.entrySet()).
  • @marstran 我的密钥只使用hashCodeequalsObject。我添加了上面的代码。我也无法更改循环,因为它是 Vaadin 框架的一部分。
  • 鉴于这是一个有些复杂的场景,如果您可以发布sscce 将会很有帮助。没什么特别的,只是一个 5 分钟的样本,它重现了 NPE
  • 感谢您的帮助。 DynamicProxyequals 方法无法正常工作。请参阅下面的答案。

标签: java hashtable vaadin


【解决方案1】:

我整理了一份 SSCCE 并进一步调查了这个问题。事实证明,这实际上根本不起作用:

    } else {
        return method.invoke(this, args); //for equals, hashCode etc.
    }

相反,我扩展了DynamicProxy,如this example所示:

    @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 不会发生。

【讨论】:

    猜你喜欢
    • 2013-04-04
    • 1970-01-01
    • 2013-10-09
    • 2012-07-06
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 2013-12-11
    • 2012-08-17
    相关资源
    最近更新 更多