NullPointerException when I try to execute runReportToPdf

0

Hi pepole,

I have a servlet that I needing  generate a report in pdf format but when executing the JasperRunManager I'm getting this nullpointerexception,
following is my servlet code and the error log:

 

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ITC_Systems_WEB_V0_Package;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.export.Exporter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.annotations.ExporterParameter;
import net.sf.jasperreports.swing.JRViewer;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Cezar Apulchro
 */
public class Emite_Receita_S001 extends HttpServlet
{
    private String      sendNome,
                        sendReceita,
                        nomepac,
                        sendCremerj,
                        botaoacao;
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        
    }

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        botaoacao = request.getParameter("acaobotao");
        
        Map parametros = new HashMap();
        
        if("Imprimir".equals(botaoacao))
        {
            DadosPaciente dpaciente = new DadosPaciente();
            nomepac = request.getParameter("nome");
            sendReceita = request.getParameter("receita");
            sendNome = request.getParameter("nomemed");
            sendCremerj = request.getParameter("crm");
            
            parametros.put("nomeParam", nomepac);
            parametros.put("receitaParam", sendReceita);
            parametros.put("nomeMedParam", sendNome);
            parametros.put("crmParam", sendCremerj);            
        }
        
        ServletOutputStream servOutputStrm = response.getOutputStream();
        InputStream inputstream = null;
        
        try
        {
            inputstream = getServletConfig().getServletContext().getResourceAsStream("C:/Receituario_S01.pdf");
            
            JasperRunManager.runReportToPdfStream(
                    inputstream, servOutputStrm, parametros, new JREmptyDataSource());
            
            response.setContentType("application/pdf");
            servOutputStrm.flush();
            servOutputStrm.close();
            
        } catch (JRException ex)
        {
            ex.printStackTrace();
            java.util.logging.Logger.getLogger(Emite_Receita_S001.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex)
        {
            ex.printStackTrace();
            
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo()
    {
        return "Short description";
    }
}
 java.lang.NullPointerException
    at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2661)
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2677)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3154)
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:862)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:358)
    at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream.<init>(ContextClassLoaderObjectInputStream.java:56)
    at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:277)
    at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:262)
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:646)
    at net.sf.jasperreports.engine.JasperRunManager.runToPdfStream(JasperRunManager.java:436)
    at net.sf.jasperreports.engine.JasperRunManager.runReportToPdfStream(JasperRunManager.java:857)
    at ITC_Systems_WEB_V0_Package.Emite_Receita_S001.doPost(Emite_Receita_S001.java:120)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    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:416)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
    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:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    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:283)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
    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:591)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
    at java.lang.Thread.run(Thread.java:748)
 

cezar_apulchro's picture
Joined: Mar 24 2013 - 5:16pm
Last seen: 2 months 2 days ago

3 Answers:

0

>inputstream = getServletConfig().getServletContext().getResourceAsStream("C:/Receituario_S01.pdf");

Check if the inputstream is null before passing to JasperRunManager

hozawa's picture
49243
Joined: Apr 24 2010 - 4:31pm
Last seen: 3 months 1 week ago
0
Hi hozawa, thanks for your assistance. The inputstream is showing as null.
cezar_apulchro's picture
Joined: Mar 24 2013 - 5:16pm
Last seen: 2 months 2 days ago
0

No news?

Cezar.

cezar_apulchro's picture
Joined: Mar 24 2013 - 5:16pm
Last seen: 2 months 2 days ago
Feedback
randomness