I need to generate a report here with Jasper but it is giving error, I did this:
Java:
public void getRelatorioNotas(List < T > lista) {
try {
FacesContext facesContext = FacesContext.getCurrentInstance();
ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();
String path = servletContext.getRealPath("/WEB-INF/report/Notas.jasper");
System.out.println(path);
InputStream stream = this.getClass().getResourceAsStream(path);
Map < String, Object > params = new HashMap < String, Object > ();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JRBeanCollectionDataSource datasrc = new JRBeanCollectionDataSource(lista);
JasperReport jasper = (JasperReport) JRLoader.loadObject(stream);
// para usar JavaBeanDataSource define 'datasrc' como datasource
JasperPrint print = JasperFillManager.fillReport(jasper, params, getConexao());
JasperExportManager.exportReportToPdfStream(print, baos);
response.reset();
response.setContentType("application/pdf");
response.setContentLength(baos.size());
response.setHeader("Content-disposition", "inline; filename=relatorioNotas.pdf");
response.getOutputStream().write(baos.toByteArray());
response.getOutputStream().flush();
response.getOutputStream().close();
context.responseComplete();
closeConnection();
} catch (Exception e) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Erro ao gerar o relatorio!"));
e.printStackTrace();
}
}
@MB
public void getRelatorio() {
RelatorioNotas < Notas > report = new RelatorioNotas < Notas > ();
if (notas.size() > 0) {
report.getRelatorioNotas(notas);
} else {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Não há registros!"));
}
}
Html:
"<p:commandButton value="Relatório"
actionListener="#{notasBean.getRelatorio()}" ajax="false"
onclick="this.form.target='_blank' "></p:commandButton>"
Error:
java.lang.NullPointerException at java.io.ObjectInputStream $ PeekInputStream.read (ObjectInputStream.java:2338) at java.io.ObjectInputStream $ PeekInputStream.readFully (ObjectInputStream.java:2351) at java.io.ObjectInputStream $ BlockDataInputStream.readShort (ObjectInputStream.java:2822) at java.io.ObjectInputStream.readStreamHeader (ObjectInputStream.java:804) at java.io.ObjectInputStream. (ObjectInputStream.java:301) at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream. (ContextClassLoaderObjectInputStream.java:58) at net.sf.jasperreports.engine.util.JRLoader.loadObject (JRLoader.java:248) at net.sf.jasperreports.engine.util.JRLoader.loadObject (JRLoader.java:233) at br.fatec.escolar.relatorio.RelatorioNotas.getRelatorioNotas (ReportNotes.java:63) at br.fatec.escolar.bean.NotasBean.getReport (NotesBean.java:77) at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke (Method.java:498) at org.apache.el.parser.AstValue.invoke (AstValue.java:247) at org.apache.el.MethodExpressionImpl.invoke (MethodExpressionImpl.java:267) at com.sun.faces.facelets.el.TagMethodExpression.invoke (TagMethodExpression.java:105) at javax.faces.event.MethodExpressionActionListener.processAction (MethodExpressionActionListener.java:147) at javax.faces.event.ActionEvent.processListener (ActionEvent.java:88) at javax.faces.component.UIComponentBase.broadcast (UIComponentBase.java:814) at javax.faces.component.UICommand.broadcast (UICommand.java:300) at javax.faces.component.UIData.broadcast (UIData.java:1108) at javax.faces.component.UIViewRoot.broadcastEvents (UIViewRoot.java:790) at javax.faces.component.UIViewRoot.processApplication (UIViewRoot.java:1282) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute (InvokeApplicationPhase.java:81) at com.sun.faces.lifecycle.Phase.doPhase (Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.execute (LifecycleImpl.java:198) at javax.faces.webapp.FacesServlet.service (FacesServlet.java:658) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java:292) at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:207) at org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java:240) at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:207) at org.apache.catalina.core.StandardWrapperValve.invoke (StandardWrapperValve.java:212) at org.apache.catalina.core.StandardContextValve.invoke (StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke (AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke (StandardHostValve.java:141) at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke (AbstractAccessLogValve.java:616) at org.apache.catalina.core.StandardEngineValve.invoke (StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service (CoyoteAdapter.java:509) at org.apache.coyote.http11.AbstractHttp11Processor.process (AbstractHttp11Processor.java:1104) at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process (AbstractProtocol.java:684) at org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun (NioEndpoint.java:1520) at org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run (NioEndpoint.java:1476) at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run (TaskThread.java:61) at java.lang.Thread.run (Thread.java:745)