简体   繁体   中英

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

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.

Creating the array with ProjectNode[] pr = new ProjectNode[10]; for example creates an empty array of length 10. Do this instead of assigning it to null.

At least you have a problem here:

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

It will throw a NullPointerException on the second line...

First line should be something like:

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

Actually your code should give you a NullPointerException because here:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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