将ManagedBean注入另一个bean失败

时间:2012-10-19 14:27:16

标签: jsf primefaces

我试图将Managed Bean注入另一个bean,但失败了。这是第一个bean:

@ManagedBean(name = "sucBean")
@SessionScoped
public class SucBean implements Serializable {

private static final long serialVersionUID = 1L;

private MapModel advancedModel;

private MapModel advancedModel2;

private Marker marker;

private Suc suc;

private List<Suc> sucDefteri;

private List<Suc> searchResult;

private Suc[] selectedSuc;

private SucService sucService;

private String aramaKriteri;

private String arananKelime;

private SucDataModel sucModel;

    // other getters/setters methods

当我运行Web应用程序时,我正在使用

Caused by: java.lang.NullPointerException
at org.primefaces.component.chart.CartesianChart.getCategories(CartesianChart.java:32)
at org.primefaces.component.chart.bar.BarChartRenderer.encodeData(BarChartRenderer.java:121)
at org.primefaces.component.chart.bar.BarChartRenderer.encodeScript(BarChartRenderer.java:51)
at org.primefaces.component.chart.bar.BarChartRenderer.encodeEnd(BarChartRenderer.java:36)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1786)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)

第二个豆子是:

@ManagedBean(name="chartBean")
@SessionScoped
public class ChartBean implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private CartesianChartModel categoryModel;

@ManagedProperty("#{sucBean}")
private SucBean sucBean;

private int[] sucSayilari=new int[9];


public ChartBean()
{
    createCategoryModel();
}

1 个答案:

答案 0 :(得分:3)

我认为这是问题所在:

public ChartBean()
{
    createCategoryModel();
}

此时未设置您的依赖项,因为JSF实现首先实例化您的bean(这意味着构造函数必须完成)然后注入其依赖项,因此如果您在createCategoryModel()中使用注入的bean将是一个空引用。

如果您需要对bean执行某些操作,请在设置依赖项后使用@PostConstruct分配的方法:

public ChartBean(){
}

@PostConstruct
public void init(){
   createCategoryModel();
}