2012-07-02 37 views
0

我正在修改內置ds的示例。這個例子的工作方式是你需要選擇一個數據源,然後一個表被填充了來自那裏的數據。這個想法是展示同一個組件如何適應多個數據源。我已經設法運行這個例子,它的工作原理,但我試圖修改它,所以你跳過第一步 - 只有一個數據源被加載到表中。令我感到困惑的是,這應該是微不足道的,但由於某種原因,它不是。我剛貼的代碼的不同部分,這一個工程:我無法加載我的SmartGWT數據源

// we create a list of datasources 
    ListGrid grid = new ListGrid(); 
    grid.setLeft(20); 
    grid.setTop(75); 
    grid.setWidth(130); 
    grid.setLeaveScrollbarGap(false); 
    grid.setShowSortArrow(SortArrow.NONE); 
    grid.setCanSort(false); 
    grid.setFields(new ListGridField("dsTitle", "Select a DataSource")); 
    // I'm just loading the one I need 
    grid.setData(new ListGridRecord[] { new DSRecord("predmeti", "predmeti")}); 
    grid.setSelectionType(SelectionStyle.SINGLE); 
    grid.addRecordClickHandler(new RecordClickHandler() { 
     public void onRecordClick(RecordClickEvent event) { 
      DSRecord record = (DSRecord) event.getRecord(); 
      bindComponents(record.getDsName()); 
     } 
    }); 

    grid.draw(); 

,這是bindComponents方法:

private void bindComponents(String dsName) { 
    DataSource ds = DataSource.get(dsName); 
    boundList.setDataSource(ds); 
    boundViewer.setDataSource(ds); 
    boundForm.setDataSource(ds); 
    boundList.fetchData(); 
    newBtn.enable(); 
    saveBtn.disable(); 
} 

和這個作品,因爲它應該。現在,因爲我只有一個數據源,我可以跳過電網,只是撥打電話給bindComponents:

bindComponents(); 

而且bindComponents看起來是這樣的:

private void bindComponents() { 
    DataSource ds = DataSource.get("predmeti"); 
    boundList.setDataSource(ds); 
    boundViewer.setDataSource(ds); 
    boundForm.setDataSource(ds); 
    boundList.fetchData(); 
    newBtn.enable(); 
    saveBtn.disable(); 
} 

我看不出爲什麼秒不起作用,它打破boundList.setDataSource(ds);。我在調試模式下對它進行了檢查,返回了一個對象,在兩種情況下它看起來都是「相同的」,但由於某種原因,它在第二個示例中不起作用,所以我猜測我太早實例化數據源或,不知何故,只是明顯錯了:)

回答

0

一切都很好的數據源,這個問題是微不足道的,我一定是累了 - 在這個調用boundList.setDataSource(ds);時的boundList對象還沒有實例化。我一直得到空指針異常,但我認爲DataSource有問題。