簡體   English   中英

現有鍵的哈希表為空

[英]Hashtable getting null for existing key

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

}

我整理了一個 SSCCE 並進一步調查了這個問題。 事實證明,這實際上根本不起作用:

    } 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM