我正在尝试设法使用以下示例MeterGauge由于xhtml代码不完整,您可以使用它来创建整个xhtml页面,因此我从PrimeFaces official doc获取了缺少的标记第12页。 然后创建了JavaBean文件ChartView.java(从MeterGauge示例页面获取代码)并尝试在GlassFish服务器上运行它,但它失败了,正如您从日志中看到的那样
metergauge2.xhtml
<!DOCTYPE html>
<html xmlns="http://www.w3c.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough">
<h:head>
</h:head>
<h:body>
<p:chart type="metergauge" model="#{chartBean.meterGaugeModel1}" style="width:400px;height:250px" />
<p:chart type="metergauge" model="#{chartBean.meterGaugeModel2}" style="width:400px;height:250px" />
</h:body>
</html>
ChartView.java
package sources;
import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import org.primefaces.model.chart.MeterGaugeChartModel;
@ManagedBean
public class ChartView implements Serializable {
private MeterGaugeChartModel meterGaugeModel1;
private MeterGaugeChartModel meterGaugeModel2;
@PostConstruct
public void init() {
createMeterGaugeModels();
}
public MeterGaugeChartModel getMeterGaugeModel1() {
return meterGaugeModel1;
}
public MeterGaugeChartModel getMeterGaugeModel2() {
return meterGaugeModel2;
}
private MeterGaugeChartModel initMeterGaugeModel() {
List<Number> intervals = new ArrayList<Number>(){{
add(20);
add(50);
add(120);
add(220);
}};
return new MeterGaugeChartModel(140, intervals);
}
private void createMeterGaugeModels() {
meterGaugeModel1 = initMeterGaugeModel();
meterGaugeModel1.setTitle("MeterGauge Chart");
meterGaugeModel1.setGaugeLabel("km/h");
meterGaugeModel2 = initMeterGaugeModel();
meterGaugeModel2.setTitle("Custom Options");
meterGaugeModel2.setSeriesColors("66cc66,93b75f,E7E658,cc6666");
meterGaugeModel2.setGaugeLabel("km/h");
meterGaugeModel2.setGaugeLabelPosition("bottom");
meterGaugeModel2.setShowTickLabels(false);
meterGaugeModel2.setLabelHeightAdjust(110);
meterGaugeModel2.setIntervalOuterRadius(100);
}
}
Glassfish记录
2015-07-17T16:51:23.536+0200|Grave: Error Rendering View[/metergauge2.xhtml]
java.lang.NullPointerException
at org.primefaces.component.chart.renderer.MeterGaugeRenderer.encodeData(MeterGaugeRenderer.java:33)
at org.primefaces.component.chart.renderer.BasePlotRenderer.render(BasePlotRenderer.java:29)
at org.primefaces.component.chart.ChartRenderer.encodeScript(ChartRenderer.java:98)
at org.primefaces.component.chart.ChartRenderer.encodeEnd(ChartRenderer.java:67)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:456)
at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:285)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)
2015-07-17T16:51:23.601+0200|Avvertenza: StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
at org.primefaces.component.chart.renderer.MeterGaugeRenderer.encodeData(MeterGaugeRenderer.java:33)
at org.primefaces.component.chart.renderer.BasePlotRenderer.render(BasePlotRenderer.java:29)
at org.primefaces.component.chart.ChartRenderer.encodeScript(ChartRenderer.java:98)
at org.primefaces.component.chart.ChartRenderer.encodeEnd(ChartRenderer.java:67)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:456)
at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:285)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:4)
只需根据堆栈跟踪检查source code。
java.lang.NullPointerException
at org.primefaces.component.chart.renderer.MeterGaugeRenderer.encodeData(MeterGaugeRenderer.java:33)
MeterGaugeRenderer#encodeData()
line 33如下:
33 writer.write(",data:[[" + model.getValue() + "]]");
此时writer
肯定不能为空。因此,从逻辑上讲,model
为null
。
仔细检查<p:chart model>
属性后,看来他们在当前形式的展示中犯了一个错误。仔细查看托管bean名称。
<p:chart ... model="#{chartBean.meterGaugeModel1}" />
@ManagedBean
public class ChartView implements Serializable {}
确实,他们不匹配。
因此,解决方案是:在视图中使用#{chartView.meterGaugeModel1}
,或将支持bean类重命名为ChartBean
。