dnvsrikanth Posted September 10, 2007 Share Posted September 10, 2007 Hi I am trying to generate dynamic report by taking already existing jrxml file and modifying the jrxml file. While generating the report in pdfstream by taking the modified report template into inputstream object I am encountering NullPointerException. But I am able to generate PDF file and HTML file. Can you please help me in resolving this problem. I am trying this error from 2 weeks.:( :sick: I am herewith providing the code: public class Replace_Tag extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ Connection con; Class.forName("net.sourceforge.jtds.jdbc.Driver"); con = DriverManager.getConnection("jdbc:jtds:sqlserver://10.2.84.71/Metrics_Commercial_152;instance=", "metric", "metric"); JasperCompileManager.compileReportToFile("chart.jrxml"); String path = this.getServletConfig().getServletContext().getRealPath("chart.jasper");//System.out.println("The path of jasper file is: " + path); InputStream inputStream = this.getServletContext().getResourceAsStream("chart.jasper"); ServletOutputStream servletOutputStream = response.getOutputStream(); JasperRunManager.runReportToPdfFile("chart.jasper", "linechart.pdf", null, con); JasperRunManager.runReportToHtmlFile("chart.jasper", "linechart.html", null, con); JasperRunManager.runReportToPdfStream(inputStream, servletOutputStream, null, con); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); //parsing the document Document doc = db.parse("chart.jrxml"); NodeList lineNodeList = doc.getElementsByTagName("lineChart"); NodeList linePlotList = doc.getElementsByTagName("linePlot"); int lineNodeListLength = lineNodeList.getLength(); int linePlotListLength = linePlotList.getLength(); for(int i=0;i<lineNodeListLength;i++){ Node lineChartNode = lineNodeList.item(i); Node renamed = doc.renameNode(lineChartNode, "", "barChart"); } for(int j=0;j<linePlotListLength;j++) { Node linePlotNode = linePlotList.item(j); Node renamedPlot = doc.renameNode(linePlotNode, "", "barPlot"); } boolean result = saveXMLDocument("barchart.jrxml", doc); JasperCompileManager.compileReportToFile("barchart.jrxml", "/reports/barchart.jasper"); inputStream = this.getServletContext().getResourceAsStream("/reports/barchart.jasper"); JasperRunManager.runReportToPdfFile("barchart.jasper", "barchart.pdf", null, con); JasperRunManager.runReportToHtmlFile("barchart.jasper", "barchart.html", null, con); JasperRunManager.runReportToPdfStream(inputStream, servletOutputStream, null, con);System.out.println("The report is generated in pdf stream"); con.close(); response.setContentType("application/html"); servletOutputStream.flush(); servletOutputStream.close(); }catch(DOMException de){ de.printStackTrace(); }catch(JRException jre){ jre.printStackTrace(); }catch(Exception e){ e.printStackTrace(); } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * @param request servlet request * @param response servlet response */ @Overrideprotected 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 */ @Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. */ @Overridepublic String getServletInfo() { return "Short description"; } // </editor-fold> public boolean saveXMLDocument(String fileName, Document doc) { System.out.println("Saving XML file... " + fileName); // open output stream where XML Document will be saved File xmlOutputFile = new File(fileName); FileOutputStream fos; Transformer transformer; try { fos = new FileOutputStream(xmlOutputFile); } catch (FileNotFoundException e) { System.out.println("Error occured: " + e.getMessage()); return false; } // Use a Transformer for output TransformerFactory transformerFactory = TransformerFactory.newInstance(); try { transformer = transformerFactory.newTransformer(); } catch (TransformerConfigurationException e) { System.out.println("Transformer configuration error: " + e.getMessage()); return false; } DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(fos); // transform source into result will do save try { transformer.transform(source, result); } catch (TransformerException e) { System.out.println("Error transform: " + e.getMessage()); } System.out.println("XML file saved."); xmlOutputFile.setExecutable(true); return true; }}Or else can u please help me in generating dynamic reports on fly based on user input. The report generation mechanism should have such a capacity to display one type of chart from another type of chart by modifying the same jrxml source. I am sorry if this is confusing u... Link to comment Share on other sites More sharing options...
yurish Posted September 11, 2007 Share Posted September 11, 2007 I made my pdfstream like this: Code://sufficient code goes hereServletOutputStream servletOutputStream = res.getOutputStream();//code for jasperReports like initialising jasperPrint...JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream); servletOutputStream.flush();servletOutputStream.close();//code ending Link to comment Share on other sites More sharing options...
dnvsrikanth Posted September 11, 2007 Author Share Posted September 11, 2007 Thanq very much...its working....:woohoo: :laugh: Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now