2012-07-13 39 views
1

我使用J2ME在Eclipse中LWUIT發展S60添加布局集裝箱環路觸發一個NullPointerException異常

我正在寫這個方法繪製列表項並嘗試手動創建列表而不是使用Lwuit列表。因爲正如我張貼在我最後一個問題here is LinK..不知道爲什麼,但它會降低性能。

所以在下面的方法我試圖創建在其中我添加添加兩個標籤到layoutX容器,並將Conatiner添加到layoutY容器並將該layoutY添加到BaseContainer,所以輸出看起來像列表。

方法是在這裏...

private void drawAgendasListItem(Vector vector) { 

     Container containerX[] = new Container[vector.size()]; 
     Container containerY[] = new Container[vector.size()]; 

     if (featuredeventsForm.contains(baseContainer)) { 
      baseContainer.removeAll(); 
      featuredeventsForm.removeComponent(baseContainer); 
      System.out.println("base Container is removed "); 
     } 

     BoxLayout layoutX = new BoxLayout(BoxLayout.X_AXIS); 
      BoxLayout layoutY = new BoxLayout(BoxLayout.Y_AXIS); 

     for (int i = 0; i < vector.size(); i++) { 

     try { 
       containerX[i].setLayout(layoutX); 
       containerY[i].setLayout(layoutY); 

       Label startTime = new Label(); 
       Label description = new Label(); 

       startTime.getStyle().setBgTransparency(0); 
       startTime.setText("start 10:20 Am"); 
       startTime.getStyle().setMargin(0, 0, 0, 5); 

       description.getStyle().setBgTransparency(0); 
       description.setText("decriptionString"); 

       containerX[i].getStyle().setPadding(0, 0, 2, 2); 
       containerX[i].addComponent(startTime); 
       containerX[i].addComponent(description); 

       containerY[i].addComponent(containerX[i]); 
       baseContainer.addComponent(i, containerX[i]); 

       System.out.println("Component added to base Container @ " + i); 

      } catch (Exception e) { 
       System.out.println("Exception in drawAgendaListItem " + e); 
      } 
     } 
     featuredeventsForm.addComponent(baseContainer); 
     featuredeventsForm.invalidate(); 
     featuredeventsForm.repaint(); 
     System.out.println("All elements added and form repainted"); 

    } 

在上述方法中,當我嘗試分配佈局集裝箱它將觸發在行 containerX[i].setLayout(layoutX);一個NullPointerException異常。

我不明白爲什麼會發生這種情況,我也嘗試對該行進行註釋,然後在行containerX[i].getStyle().setPadding(0, 0, 2, 2);處觸發NullPointerException。

請幫忙....

回答

1

基於源代碼,我的猜測是,你認爲實例化一個數組也填充它。在Java中情況並非如此。

換句話說,如果你認爲containerX是這樣的:在內存
[new Container, new Container,..., new Container]
,這是不正確。它實際上是這樣的:
[null,null,...,null]

我認爲你需要在循環的開頭添加

containerX[i] = new Container();
containerY[i] = new Container();

(也許你要實例化數組的內容作爲容器的子類)

+0

好的,我想它。現在.. – MobileEvangelist 2012-07-13 10:14:14

+0

我加了這幾行, containerX [i] = new Container(layoutX); containerY [i] = new Container(new BoxLayout(BoxLayout.Y_AXIS)); 但它仍然無法正常工作。同樣的錯誤火災 – MobileEvangelist 2012-07-13 10:25:24

+0

我張貼在我最後一個問題列表呈現[這裏是LinK ..](http://stackoverflow.com/questions/11414861/in-listcellrenderer-getlistcellrenderercomponent-method-called-more-than-once ) – MobileEvangelist 2012-07-13 10:27:05

相關問題