2012-11-09 48 views
3

我正在使用LWUIT(1.5)的最新版本。我在資源編輯器中設計了我的表單,然後將代碼生成爲netbeans。問題是,如果我想要訪問的任何對象,除了形式,我得到這個錯誤:無法訪問除窗體外的任何對象,NullPointerException錯誤

java.lang.NullPointerException at userclasses.StateMachine.onCtnCalculation_BtnAddAction(StateMachine.java:38) at generated.StateMachineBase.handleComponentAction(StateMachineBase.java:712) at com.sun.lwuit.util.UIBuilder$FormListener.actionPerformed(UIBuilder.java:2231) at com.sun.lwuit.util.EventDispatcher.fireActionSync(EventDispatcher.java:312) at com.sun.lwuit.util.EventDispatcher.fireActionEvent(EventDispatcher.java:257) at com.sun.lwuit.Button.fireActionEvent(Button.java:364) at com.sun.lwuit.Button.released(Button.java:395) at com.sun.lwuit.Button.released(Button.java:384) at com.sun.lwuit.Button.keyReleased(Button.java:413) at com.sun.lwuit.Form.keyReleased(+64) at com.sun.lwuit.Display.handleEvent(Display.java:1533) at com.sun.lwuit.Display.edtLoopImpl(Display.java:826) at com.sun.lwuit.Display.mainEDTLoop(Display.java:776) at com.sun.lwuit.RunnableWrapper.run(RunnableWrapper.java:119)

這是我的代碼:

protected void onCtnCalculation_BtnAddAction(Component c, ActionEvent event) { 
     // If the resource file changes the names of components this call will break notifying you that you should fix the code 
     super.onCtnCalculation_BtnAddAction(c, event); 
     Form root = Display.getInstance().getCurrent(); 
     TextField txtAmount = findTxtAmount(root); 
     fltAmount += Float.parseFloat(txtAmount.getText()); 
    } 

也是我想這:

fltAmount += Float.parseFloat(findTxtAmount(root).getText()); 

回答

1

是文本數量字段是在當前表單或不同表單中的組件。如果它是不同的形式,那麼這將不起作用。

+0

我只有一個窗體,裏面有一個帶有4個選項卡的選項卡,每個選項卡都有一個嵌入式容器,並且此字段位於我的第一個選項卡中。 – Ali

+0

如果您有嵌入式容器,則必須在嵌入容器中提供一個組件才能找到文本字段,因爲它不會在表單中列出。您可以獲取選項卡並從那裏獲取特定的嵌入容器,並將其傳遞給find函數。 –