2013-03-31 77 views
0

我想從選擇一個菜單中選擇自定義對象。它既不顯示錯誤也不顯示值。該怎麼辦?請幫幫我。提前致謝。選擇自定義對象選擇一個菜單JSF轉換器異常

現在我在的getAsObject看到空指針異常這一行:

return getCurrencyService().getCurrencyBtId(currencyId); 

這是我的XHTML文檔

<h:panelGrid columns="2"> 
    <p:outputLabel value="" /> 
     <p:selectOneMenu id="CurrencyMenu" value="#{CurrencyMB.currency}" converter="currencyConverter" > 
     <f:selectItems value="#{CurrencyMB.currencyList}" var="currency" itemValue="#{currency}" itemLabel="#{currency.currencyName}" >  
      </f:selectItems> 
     <p:ajax update="currencyOut" /> 
     </p:selectOneMenu> 
     <p:outputLabel value="Currency Id : #{CurrencyMB.currency.currencyId}" id="currencyOut" /> 
</h:panelGrid> 

這是我managedBean類。

@ManagedBean(name = "CurrencyMB") 
@RequestScoped 
public class CurrencyManagedBean implements Serializable{ 

private Currency currency; 
private List<Currency> currencyList; 


public Currency getCurrency() { 
     return currency; 
    } 

public void setCurrency(Currency currency) { 
     this.currency = currency; 
    } 
public List<Currency> getCurrencyList() { 
     currencyList = new ArrayList<Currency>(); 
     currencyList.addAll(getiCurrencyService().getCurrencies()); 
     return currencyList; 
    } 

public void setCurrencyList(List<Currency> currencyList) { 
     this.currencyList = currencyList; 
    } 

} 

這是我currencyConverter類代碼:

**@FacesConverter("currencyConverter") 
public class CurrencyConverter implements Converter { 
    @ManagedProperty(value = "#{currencyService}") 
    private ICurrencyService currencyService; 


    public ICurrencyService getCurrencyService() { 
     return currencyService; 
    } 
    public void setCurrencyService(ICurrencyService currencyService) { 
     this.currencyService = currencyService; 
    } 
    @Override 
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) { 

     int currencyId = Integer.valueOf(value); 
     return getCurrencyService().getCurrencyBtId(currencyId); 
    } 
    @Override 
    public String getAsString(FacesContext arg0, UIComponent arg1, Object value) { 
     Currency currency = (Currency) value; 
     int currencyId = currency.getCurrencyId(); 
     return String.valueOf(currencyId); 
    } 
}** 

回答

0

因爲它可以在許多問題中可以看出,他們的答案,在這裏,@FacesConverter不符合注射。在你的情況下,你試圖通過@ManagedProperty注入你的服務,這會產生NPE,正如Luiggi在你對最初問題的評論中告訴你的。

在當前的JSF版本,你可以做你的轉換器注入的服務是放棄使用@FacesConverter並切換到使用標準@ManagedBean註釋,從而涉及到您轉換器不是ID,而是由bean的名稱,像converter="#{currencyConverter}"

+0

它不以任何方式工作。 :( – Umair

+0

)如果你對所寫的內容付出足夠的注意力,你將能夠得到答案,只需一步一步完成即可解決問題。 – skuntsel