2013-02-19 56 views
0

以下Codesnipplet將引發一個。它由findByName方法拋出。看起來像找不到PlayerComponentGroupList,但我不知道爲什麼。findByName NullPointerException

我該如何參考生成的

protected void beforeMain(Form f) { 
    super.beforeMain(f); 
    try { 
     findPlayerComponentGroup(f).addComponent(getItems("Player")); 
    } catch (IOException ex) { 
     throw new RuntimeException(ex.getMessage()); 
    } 

} 

private Container getItems(String order) throws IOException { 
... 
final Container rows = new ComponentGroup(); 
... 
rows.setName("PlayerComponentGroupList"); 
    return rows; 
} 

protected void onMain_PlayerContainerButtonSortNameAction(Component c, ActionEvent event) { 
    super.onMain_PlayerContainerButtonSortNameAction(c, event); 
    try { 
     findPlayerComponentGroup().removeComponent(findByName("PlayerComponentGroupList", Display.getInstance().getCurrent()))); 
... 
} 
+0

哪個方法拋出異常?爲什麼不在調試器中運行它並查看返回null的內容? – 2013-02-20 08:57:28

+0

對不起,沒有制定這個popper:findByName ....修正了問題 – Megachip 2013-02-20 13:39:19

回答

0

findByName()不工作的方式。它只適用於由GUI構建器創建的組件,因此速度非常快(否則它將不得不遞歸搜索非常慢的組件樹)。

您需要保留一個指向組件的指針並將其刪除。

+0

請問你可以添加一個簡短的例子嗎? THX – Megachip 2013-02-21 13:05:12

+0

解決方法我通過GUI構建器添加了一個空容器,我可以使用remove all和readd所需的組件。希望這是一個可接受的解決方案 – Megachip 2013-02-21 14:16:43

+0

是的,這是有道理的。 – 2013-02-21 15:02:28