簡體   English   中英

為什么我在此代碼中出現“無法創建組件”錯誤?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM