1

我尝试将托管 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)

第二个bean如下:

@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();
}
4

1 回答 1

3

我认为这是问题所在:

public ChartBean()
{
    createCategoryModel();
}

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

如果您需要在设置依赖项之后对 bean 执行某些操作,请使用带有注释的方法@PostConstruct

public ChartBean(){
}

@PostConstruct
public void init(){
   createCategoryModel();
}
于 2012-10-19T14:36:57.910 回答