[英]Why do I get “Component cannot be created” error in this code?
我想创建BeanTreeView的新节点,当我在构造函数中添加一些节点,然后运行应用程序,然后我尝试用树查看窗口,它会抛出此错误
java.lang.AssertionError: Component cannot be created for {component=null, displayName=Exploirer, instanceCreate=AlwaysEnabledAction[Exploirer]} at org.openide.windows.OpenComponentAction.getTopComponent(OpenComponentAction.java:71)
为什么? 以及如何在那里添加节点? 看代码。
private ProjectsChildren projectsChildren;
private final ExplorerManager mgr = new ExplorerManager();
private ProjectNode projectNode = new ProjectNode(new MainProject("ggg"), projectsChildren);
public ExploirerTopComponent() {
//*****************This is not important code for my problem
initComponents();
setName(NbBundle.getMessage(ExploirerTopComponent.class, "CTL_ExploirerTopComponent"));
setToolTipText(NbBundle.getMessage(ExploirerTopComponent.class, "HINT_ExploirerTopComponent"));
// setIcon(ImageUtilities.loadImage(ICON_PATH, true));
//map.put("delete", ExplorerUtils.actionDelete(mgr, true));
//*******************end of not important code
associateLookup (ExplorerUtils.createLookup(mgr, getActionMap()));
/* somewhere here is the problem*/
mgr.setRootContext(projectNode);
ProjectNode[] pr = null;
pr[0] = projectNode;
mgr.getRootContext().getChildren().add(pr);
}
ProjectNode[] pr = null;
pr[0] = projectNode;
也许这就是问题所在? 您有一个指向null的数组指针,然后您尝试将第一个ProjectNode对象分配给不存在的数组。
使用ProjectNode[] pr = new ProjectNode[10];
创建数组ProjectNode[] pr = new ProjectNode[10];
例如,创建一个长度为10的空数组。执行此操作而不是将其指定为null。
至少你有一个问题:
ProjectNode[] pr = null;
pr[0] = projectNode;
它将在第二行抛出NullPointerException ...
第一行应该是这样的:
ProjectNode[] pr = new ProjectNode[5]; // size is 5
实际上你的代码应该给你一个NullPointerException
因为这里:
ProjectNode[] pr = null;
pr[0] = projectNode;
首先将数组设置为null,然后尝试访问第0个元素,然后将其设置为projectNode
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.