JasperReports with Vaadin ?

I’m looking for a solution to create reports using JasperReports for my application. I found some examples but still could not make it work.

I’m using:
Maven
Vaadin-7
JasperReports-5.51

I’m trying this.


public class Report {
    
    public Report(){
        createShowReport();
    }
    
    private void createShowReport(){
        String reportFile = "reports/report3.jasper";
        InputStream file = getClass().getClassLoader().getResourceAsStream(reportFile);        
        Map parametros = new HashMap();
        
        try {
            List<String> lista = new ArrayList<String>();
            lista.add("Fernando");
            lista.add("Paiva");
            JRDataSource dados = new JRBeanCollectionDataSource(lista);
            JasperPrint printer = JasperFillManager.fillReport(file, parametros,dados);
            JRViewer view = new JRViewer(printer);
            
            
        } catch (JRException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }    
    }
    
    private Window getWindow(){
        Window w = new Window();
        w.setSizeFull();
        w.setModal(true);
        w.setVisible(true);
        return w;
    }
}

The method getWindow() should return a Window to add my report and show modal.

/** Exception */


SEVERE: 
com.vaadin.server.ServerRpcManager$RpcInvocationException: Unable to invoke method click in com.vaadin.shared.ui.button.ButtonServerRpc
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:170)
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:118)
    at com.vaadin.server.communication.ServerRpcHandler.handleBurst(ServerRpcHandler.java:207)
    at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:111)
    at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:91)
    at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:37)
    at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1382)
    at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:238)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
    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:724)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:168)
    ... 27 more
Caused by: com.vaadin.event.ListenerMethod$MethodException: Invocation of method buttonClick in br.ind.ibg.views.ViewCurriculum failed.
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:528)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
    at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:969)
    at com.vaadin.ui.Button.fireClick(Button.java:368)
    at com.vaadin.ui.Button$1.click(Button.java:57)
    ... 32 more
Caused by: java.lang.NullPointerException
    at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2308)
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2321)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2792)
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:799)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
    at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream.<init>(ContextClassLoaderObjectInputStream.java:58)
    at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:244)
    at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:229)
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:632)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:956)
    at br.ind.ibg.reports.Report.createShowReport(Report.java:34)
    at br.ind.ibg.reports.Report.<init>(Report.java:21)
    at br.ind.ibg.views.ViewCurriculum.buttonClick(ViewCurriculum.java:413)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
    ... 37 more

How to create reports using JasperReports with Vaadin ?

Your stack trace doesn’t match the code you posted

    at br.ind.ibg.reports.Report.createShowReport(Report.java:34)
    at br.ind.ibg.reports.Report.<init>(Report.java:21)

But I would take a guess that you did not configure everything for the report…

Make sure that your resource is loaded properly , getResourceAsStream can return null, also you should take care of using getClass().getClassLoader().

   InputStream file = getClass().getClassLoader().getResourceAsStream(reportFile);  

Refere to
http://www.javaworld.com/article/2077344/core-java/find-a-way-out-of-the-classloader-maze.html
for some notes on that issue…

hello

I did: String reportFile = “br/ind/ibg/reports/report3.jasper”;
And loaded the file, but now I can’t view the report

I’m trying this.


public class Report {
    
    public Report(){
        createShowReport();
    }
    
    private void createShowReport(){
        String reportFile = "br/ind/ibg/reports/report3.jasper";
        InputStream file = getClass().getClassLoader().getResourceAsStream(reportFile);                
        Map parametros = new HashMap();
        
        try {
            List<String> lista = new ArrayList<String>();
            lista.add("Fernando");
            lista.add("Paiva");
            JRDataSource dados = new JRBeanCollectionDataSource(lista);
            JasperPrint printer = JasperFillManager.fillReport(file, parametros,dados);
            JRViewer view = new JRViewer(printer);
            
            Embedded e = new Embedded();                                
            e.setData(view);            
            getWindow().setContent(e);
            
            UI.getCurrent().addWindow(getWindow());
        } catch (JRException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }    
        
        
    }
    
    private Window getWindow(){
        Window w = new Window("Abriiiiii...");
        w.setSizeFull();
        w.setModal(true);        
        w.center();
        return w;
    }

}

Any idea ?