sách gpt4 ai đã đi

java - 现有 key 的哈希表为空

In lại 作者:行者123 更新时间:2023-11-30 10:39:00 27 4
mua khóa gpt4 Nike

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

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

调试器显示这个状态: cửa sổ

这怎么可能? object2 如何为 vô giá trị?

这是导致 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);
} khác {
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")
@Ghi đè
public T createDTO(Class type) {
return (T) Proxy.newProxyInstance(
type.getClassLoader(),
new Class[] { type },
new DynamicProxy());
}

private static class DynamicProxy implements InvocationHandler {

private Map properties = new HashMap<>();

@Ghi đè
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if(method.getName().startsWith("set")) {
properties.put(method.getName().substring(3), args[0]);
trả về giá trị 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));
} khác {
return method.invoke(this, args); //for equals, hashCode etc.
}
}
}

}

1 Câu trả lời

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

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

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

    @Ghi đè
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if(method.getName().startsWith("set")) {
properties.put(method.getName().substring(3), args[0]);
trả về giá trị 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;
} khác {
throw new IllegalStateException(String.valueOf(method));
}
}

这样 hashCode Đặc biệt equals 可以按预期工作,NPE 不会发生。

关于java - 现有 key 的哈希表为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39448560/

27 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com