简体   繁体   English

为什么我在此代码中出现“无法创建组件”错误?

[英]Why do I get “Component cannot be created” error in this code?

I want to create new node of BeanTreeView, and when I add some node in constructor, then run the app, and then I try to view the window with tree, it throws this error 我想创建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)

Why? 为什么? And how to add node there? 以及如何在那里添加节点? See the code. 看代码。

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;

Perhaps this is the problem here? 也许这就是问题所在? You have an array pointer that points to null and then you try to assign the first ProjectNode object to a non-existent array. 您有一个指向null的数组指针,然后您尝试将第一个ProjectNode对象分配给不存在的数组。

Creating the array with ProjectNode[] pr = new ProjectNode[10]; 使用ProjectNode[] pr = new ProjectNode[10];创建数组ProjectNode[] pr = new ProjectNode[10]; for example creates an empty array of length 10. Do this instead of assigning it to null. 例如,创建一个长度为10的空数组。执行此操作而不是将其指定为null。

At least you have a problem here: 至少你有一个问题:

ProjectNode[] pr = null;
pr[0] = projectNode;

It will throw a NullPointerException on the second line... 它将在第二行抛出NullPointerException ...

First line should be something like: 第一行应该是这样的:

 ProjectNode[] pr = new ProjectNode[5]; // size is 5

Actually your code should give you a NullPointerException because here: 实际上你的代码应该给你一个NullPointerException因为这里:

ProjectNode[] pr = null;
pr[0] = projectNode;

first you set the array to null and then you try to access the 0-th element so set it to projectNode . 首先将数组设置为null,然后尝试访问第0个元素,然后将其设置为projectNode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM