我正在尝试使用PrimeFaces(V8.0/7.0(生成饼图。下面是演示代码。注意-我正在Glassfish5 上运行此演示
import javax.annotation.PostConstruct;
import javax.inject.Named;
import org.primefaces.model.chart.PieChartModel;
@Named
public class PieDemoMBean {
private PieChartModel model;
@PostConstruct
public void init(){
model=new PieChartModel();
model.set("Red", 25);
model.set("Green", 25);
model.set("Blue", 25);
model.set("Black", 25);
model.setShowDataLabels(true);
}
public PieChartModel getModel() {
return model;
}
}
和JSF页面
<html xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h1>Pi Chart Demo</h1>
<div>
<p:chart type="pie" responsive="true" model="#{pieDemoMBean.model}"/>
</div>
</h:body>
</html>
最后是错误日志
Error Rendering View[/PiDemo.xhtml]
java.lang.NullPointerException
at org.primefaces.component.chart.renderer.PieRenderer.encodeData(PieRenderer.java:43)
at org.primefaces.component.chart.renderer.BasePlotRenderer.render(BasePlotRenderer.java:38)
请告知。
现在问题已经解决,因为我对代码进行了一些更改,并将饼图构造从init((方法移到了getModel((方法,如下所示-
@PostConstruct
public void init(){
}
public PieChartModel getModel() {
model=new PieChartModel();
model.set("Red", 25);
model.set("Green", 25);
model.set("Blue", 25);
model.set("Black", 25);
model.setShowDataLabels(true);
return model;
}
而且新代码运行得很好!!(尽管我很困惑,因为托管Bean是RequestScoped,init((方法中的代码应该同样有效(