我在我的WebProject中使用BubbleChart
(来自primefaces 4.0):
我的托管bean:
package echallenge_hrm_project.ManagedBean;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import org.primefaces.model.chart.BubbleChartModel;
import org.primefaces.model.chart.BubbleChartSeries;
@ManagedBean(name="chartBean")
public class ChartBean implements Serializable {
private BubbleChartModel bubbleModel;
public ChartBean() {
createBubbleModel();
}
private void createBubbleModel() {
BubbleChartModel bubbleModel = new BubbleChartModel();
bubbleModel.add(new BubbleChartSeries("Acura", 70, 183, 55));
bubbleModel.add(new BubbleChartSeries("Alfa Romeo", 45, 92, 36));
bubbleModel.add(new BubbleChartSeries("AM General", 24, 104, 40));
bubbleModel.add(new BubbleChartSeries("Bugatti", 50, 123, 60));
bubbleModel.add(new BubbleChartSeries("BMW", 15, 89, 25));
bubbleModel.add(new BubbleChartSeries("Audi", 40, 180, 80));
bubbleModel.add(new BubbleChartSeries("Aston Martin", 70, 70, 48));
}
public BubbleChartModel getBubbleModel() {
return bubbleModel;
}
}
我的html页面:
<p:bubbleChart id="sample" value="#{chartBean.bubbleModel}" xaxisLabel="Price" yaxisLabel="Labels"
title="Sample Bubble Chart" style="width:400px;height:300px" />
我的错误堆栈跟踪:
Grave: Servlet.service() for servlet [facesServlet] in context with path [/echallenge_hrm_project] threw exception [null] with root cause
java.lang.NullPointerException
at org.primefaces.component.chart.bubble.BubbleChartRenderer.encodeData(BubbleChartRenderer.java:65)
at org.primefaces.component.chart.bubble.BubbleChartRenderer.encodeScript(BubbleChartRenderer.java:51)
at org.primefaces.component.chart.bubble.BubbleChartRenderer.encodeEnd(BubbleChartRenderer.java:36)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:665)
at javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:545)
at javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:541)
at javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:541)
at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1981)
at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:285)
at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:116)
at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:241)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:199)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
如何解决NPE?
答案 0 :(得分:3)
您的private BubbleChartModel bubbleModel;
永远不会被初始化。
错误位于托管bean类中,您使用的是新变量。使用此属性更改createBubbleModel
函数,我们使用私有对象bubbleModel而不是创建新对象。
private void createBubbleModel() {
bubbleModel = new BubbleChartModel();
bubbleModel.add(new BubbleChartSeries("Acura", 70, 183, 55));
bubbleModel.add(new BubbleChartSeries("Alfa Romeo", 45, 92, 36));
bubbleModel.add(new BubbleChartSeries("AM General", 24, 104, 40));
bubbleModel.add(new BubbleChartSeries("Bugatti", 50, 123, 60));
bubbleModel.add(new BubbleChartSeries("BMW", 15, 89, 25));
bubbleModel.add(new BubbleChartSeries("Audi", 40, 180, 80));
bubbleModel.add(new BubbleChartSeries("Aston Martin", 70, 70, 48));
}