Why is there a "Component cannot be created" error in this code?

I want to create a new node from BeanTreeView, and when I add the node constructor to the constructor, run the application, and then I try to view the window with the tree, it gives 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 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);
  }
+3
source share
3 answers
   ProjectNode[] pr = null;
   pr[0] = projectNode;

, ? , null, ProjectNode .

ProjectNode[] pr = new ProjectNode[10];, , 10. null.

0

, :

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

NullPointerException ...

:

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

Actually your code should give you NullPointerExceptionbecause here:

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

first you set the array to null, and then try to access the 0th element, so set it to projectNode.

0
source

Source: https://habr.com/ru/post/1738674/


All Articles