<p:piechart>无法使用primefaces </p:piechart>

时间:2012-02-27 07:18:53

标签: jsf-2 primefaces

我正在为Web项目使用JSF 2.0 + Prime面孔。

我的xhtml代码是这样的:

   <p:pieChart id="sample" value="#{chartBean.pieModel}" legendPosition="w"  
                    title="Sample Pie Chart" style="width:400px;height:300px" />

Java文件代码是这样的:

public class ChartBean implements Serializable {  

    private PieChartModel pieModel;  

    public ChartBean() {  
        createPieModel();  
    }  

    public PieChartModel getPieModel() {  
        return pieModel;  
    }  

    private void createPieModel() {  
        pieModel = new PieChartModel();  

        pieModel.set("Brand 1", 540);  
        pieModel.set("Brand 2", 325);  
        pieModel.set("Brand 3", 702);  
        pieModel.set("Brand 4", 421);  
    }  
}

任何时候运行此代码都会出现此错误:

java.lang.NullPointerException
    org.primefaces.component.chart.pie.PieChartRenderer.encodeData(PieChartRenderer.java:68)
    org.primefaces.component.chart.pie.PieChartRenderer.encodeScript(PieChartRenderer.java:51)
    org.primefaces.component.chart.pie.PieChartRenderer.encodeEnd(PieChartRenderer.java:36)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)
    javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:57)
    org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:45)
    org.primefaces.component.layout.LayoutUnitRenderer.encodeEnd(LayoutUnitRenderer.java:51)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
    com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)

这是如何引起的?如何解决?

4 个答案:

答案 0 :(得分:1)

模型似乎是null。你有createPieModel方法吗?您可以在创建图表时验证/调试饼模型是否为空?

答案 1 :(得分:1)

在我的情况下,我通过导入正确的包解决了空指针异常。

我有以下

@Named("chartBean")
@ViewScoped
public class ChartBean implements Serializable 
{
    private static final long serialVersionUID = 1L;
    private PieChartModel model;

    public ChartBean() 
    {
     model = new PieChartModel();
     model.set("Brand 1", 540);
     model.set("Brand 2", 325);
     model.set("Brand 3", 702);
     model.set("Brand 4", 421);
    }

    public PieChartModel getModel() 
    {
     return model;
    }
}

但是当我导入包时,我错误地选择了javax.faces.bean.ViewScoped。 当我改为javax.faces.view.ViewScoped时,它解决了这个问题。

答案 2 :(得分:0)

XHTML页面:

<p:pieChart id="sample" value="#{yourView.chartBean.pieModel}" legendPosition="w"  
                title="Sample Pie Chart" style="width:400px;height:300px" />

在您的视图中(仅作为延迟初始化的示例):

private ChartBean chartBean;

public ChartBean getChartBean() {
    if (chartBean == null) {
    chartBean = new ChartBean();
    }
    return chartBean;
}
public void setChartBean(ChartBean chartBean) {
    this.chartBean = chartBean;
}

答案 3 :(得分:0)

你应该在你的java bean中使用这样的东西。

@ManagedBean

@ViewScoped

public class ChartBean implements Serializable {  

    private PieChartModel pieModel;  

    public ChartBean() {  
        createPieModel();  
    } 
 ....