Reputation: 83
I am using JSF 2.0 + Prime faces for web project.
My xhtml code is this :
<p:pieChart id="sample" value="#{chartBean.pieModel}" legendPosition="w"
title="Sample Pie Chart" style="width:400px;height:300px" />
And Java file code is this:
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);
}
}
Any while going to run this code getting this error :
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)
How is this caused and how can I solve it?
Upvotes: 1
Views: 5779
Reputation: 11
In my case I solved the null pointer exception by importing the correct package.
I had the following
@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;
}
}
But when I Imported the package I selected wrongly javax.faces.bean.ViewScoped. When I changed for javax.faces.view.ViewScoped, it solved the issue.
Upvotes: 1
Reputation: 566
You should use like this in your java bean.
@ManagedBean
@ViewScoped
public class ChartBean implements Serializable {
private PieChartModel pieModel;
public ChartBean() {
createPieModel();
}
....
Upvotes: 0
Reputation: 1
XHTML page:
<p:pieChart id="sample" value="#{yourView.chartBean.pieModel}" legendPosition="w"
title="Sample Pie Chart" style="width:400px;height:300px" />
In your View (Just a example with lazy initialize):
private ChartBean chartBean;
public ChartBean getChartBean() {
if (chartBean == null) {
chartBean = new ChartBean();
}
return chartBean;
}
public void setChartBean(ChartBean chartBean) {
this.chartBean = chartBean;
}
Upvotes: 0
Reputation: 30025
It seems that the model is null
. Is you createPieModel
method called at all? Can you verify/debug that the pie model is not null at the time the chart is created?
Upvotes: 1