1
I really need to generate a report here with the Jasper but it’s making a mistake, I did it:
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.com.fatec.escolar.relatorio.RelatorioNotas.getRelatorioNotas(Reportersnotes.java:63) at br.com.fatec.escolar.bean.NotasBean.getRelatorio(Notasbean.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.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.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)
the variable stream is null (this.getClass().getResourceAsStream(path) returned null). In which directory structure is your file ?
– wryel
it is within the WEB-INF (/WEB-INF/report/Notes.Jasper)
– Junior Abreu
now that I’ve seen it, change this.getClass(). getResourceAsStream(path); by servletContext.getResourceAsStream("WEB-INF/report/Notes.Jasper"); (no bar at the very beginning) it should work.
– wryel
Still giving java.lang.Nullpointerexception... I don’t know pqq
– Junior Abreu
takes a print of your expanded file structure and attaches to the question, it should work.
– wryel
I managed to find the way... now he opens the whole blank report page
– Junior Abreu
If it’s opening in blank, somehow the objects aren’t getting into Jeremiah. Note this command of yours: Jasperfillmanager.fillReport(Jasper, params, getConexao()), the third parameter should be the datasrc variable if you are using java objects as source.
– wryel
@Juniorabreu, see what was answered here: Jasper creates Empty PDF. In my environment, I put the title and it worked! Appeared the title band of iReport appeared, but the gang of detail, no!
– pss1suporte