olá pessoal
tenho um projeto web em jsp
e criei um relatorio no ireport para teste, sem select
na mesma pasta “servlet” do meu projeto tenho o arquivo executa.java e o Zonas.jasper
na hora de executar dá a mensagem que nao encontra o arquivo
caminho null
java.lang.NullPointerException
fim
segue abaixo meu arquivo executa.java
package Servelts;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.HashMap;
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.JRException;
import net.sf.jasperreports.engine.JasperRunManager;
import Classes.Conectar;
public class Executa extends HttpServlet {
public Executa() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletOutputStream servletOutputStream = response.getOutputStream();
String relatorio = "Zonas.jasper" ;
InputStream reportStream = getServletConfig().getServletContext().getResourceAsStream(relatorio);
System.out.println("caminho " + reportStream );
try {
Conectar conectar = new Conectar();
Connection c = conectar.getConnection();
HashMap parametros = new HashMap();
JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, parametros, c);
response.setContentType("application/pdf");
System.out.println("deu" );
servletOutputStream.flush();
servletOutputStream.close();
if (c != null) c.close();
} catch (JRException e) {
System.out.println( e );
//e.printStackTrace();
} catch (Exception e) {
System.out.println( e );
//e.printStackTrace();
}
System.out.println("fim" );
}
}
fazendo algumas alterações agora aparece o seguinte erro
java.io.FileNotFoundException: C:\Program%20Files\Eclipse\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\SSE\WEB-INF\classes\Relatorios\Zonas.jasper (O sistema não pode encontrar o caminho especificado)
java.io.FileInputStream.open(Native Method)
ServletOutputStream sOutputStream = response.getOutputStream();
String caminho = "/Relatorios/";
String relatorio = caminho + "Zonas.jasper";
java.net.URL resource = this.getClass().getClassLoader().getResource(relatorio);
InputStream reportStream = new FileInputStream(resource.getFile());
try {
HashMap<String, String> parametros = new HashMap<String, String>();
//parametros.put("nome", "Joaquim");
JasperRunManager.runReportToPdfStream(reportStream, sOutputStream, parametros);
response.setContentType("application/pdf");
sOutputStream.flush();
sOutputStream.close();
System.out.println("pdf gerado");
} catch (JRException e){
StringWriter sWriter = new StringWriter();
PrintWriter pWriter = new PrintWriter(sWriter);
e.printStackTrace(pWriter);
response.setContentType("text/plain");
response.getOutputStream().print(sWriter.toString());
System.out.println("txt gerado");
}
e tentando de outra maneira volta o erro null
java.lang.NullPointerException
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:89)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:601)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:517)
at Servelts.Executa.doPost(Executa.java:182)
String fileName = "C:\\Program Files\\Eclipse\\workspace\\SSE\\src\\Servelts\\Zonas.jasper";
String outFileName = "C:\\Program Files\\Eclipse\\workspace\\SSE\\src\\Servelts\\Zonas.pdf";
HashMap hm = new HashMap();
try {
JasperPrint print = JasperFillManager.fillReport(fileName,hm,new JREmptyDataSource());
JRExporter exporter = new net.sf.jasperreports.engine.export.JRPdfExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,outFileName);
exporter.setParameter(JRExporterParameter.JASPER_PRINT,print);
exporter.exportReport();
System.out.println("Created file: " + outFileName);
} catch (JRException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}