crystal report with java (jsf, hibernate)

I'm a Crystal Reports newbie, I want to write a simple java application that is print my data (using hibernate and jsf).
This is my method in my managebean:

public void print(List<Child> list) {
            
            try {
                  
                  // Instantiate the report object
                  String report_Location;
                  report_Location = "/com/reports/Report1.rpt";
                  
                  // This interface provides a common service point for getting a report source that is used to view and export reports.
                  IReportSourceFactory2 rptSrcFactory = new JPEReportSourceFactory();
                  
                  // It is recommended to use the ReportClientDocument to obtain an IReportSource object.
                  // it's also Creates a new reportsource for the report specified.
                  Object reportSource = null;
                  reportSource = rptSrcFactory.createReportSource(report_Location, java.util.Locale.getDefault());
                  
                  // creating a Fields object, populating it with the parameter values then adding that to the viewer.
              CrystalReportViewer viewer = new CrystalReportViewer();
              
              /*
               * Before parameter fields can be set in a report, the fields must first be created and initialized.
               * Individual parameter fields are all stored in a Fields object.
               * The Fields object is simply a collection of different fields that can be passed to the viewer.
               */
              
              // Create a Fields object to store the parameter fields in.
              // Returns an object that contains the parameters in the report.
              Fields<ParameterField> parameterFields;
              
              parameterFields = viewer.getParameterFields();
              
              // Clear previous parameter
              parameterFields.clear();
      
              // Create a ParameterField object for each field that you wish to set.
              ParameterField paramField_1 = new ParameterField();
              ParameterField paramField_2 = new ParameterField();
              ParameterField paramField_3 = new ParameterField();
        
              // You must set the report name.
              // Set the report name to an empty string if your report does not contain a subreport; otherwise, the report name will be the name of the subreport
              /*
               * You also must set the parameter field’s report name to the report name associated with the parameter.
               * If the parameter field is for the main report, use a blank string ("").
               * The report name is set using the setReportName method.
               */
              paramField_1.setReportName("");
      
              // Create a Values object and a ParameterFieldDiscreteValue object for each parameter field you wish to set.
              Values value_1 = new Values();
              Values value_2 = new Values();
              Values value_3 = new Values();
              
              // If a ranged value is being set, a ParameterFieldRangeValue object should be used instead of the discrete value object.
              
//              ParameterFieldDiscreteValue paramFieldDV_1 = new ParameterFieldDiscreteValue();
//              ParameterFieldDiscreteValue paramFieldDV_2 = new ParameterFieldDiscreteValue();
//              ParameterFieldDiscreteValue paramFieldDV_3 = new ParameterFieldDiscreteValue();
              
              ParameterFieldRangeValue paramFieldDV_1 = new ParameterFieldRangeValue();
              ParameterFieldRangeValue paramFieldDV_2 = new ParameterFieldRangeValue();
              ParameterFieldRangeValue paramFieldDV_3 = new ParameterFieldRangeValue();
              
              //----------- Initialize the parameter fields -----------//
              
              // Set the name and value for each parameter field that is added.
              // Values for parameter fields are represented by a ParameterFieldDiscreteValue or ParameterFieldRangeValue object.
              // "paramField_1" ("id") is parameter name in my Crystal Report file (Report1.rpt)
              
              paramField_1.setName("id");
              paramFieldDV_1.setBeginValue(child_id());
              
              paramField_2.setName("name");
              paramFieldDV_2.setBeginValue(child_name());
              
              paramField_3.setName("age");
              paramFieldDV_3.setBeginValue(child_age());
      
              // Add the parameter field values to the Values collection object.
              value_1.add(paramFieldDV_1);
              value_2.add(paramFieldDV_2);
              value_3.add(paramFieldDV_3);
      
              // Set the current Values collection for each parameter field.
              paramField_1.setCurrentValues(value_1);
              paramField_2.setCurrentValues(value_2);
              paramField_3.setCurrentValues(value_3);
      
              // Add each parameter field to the Fields collection.
              // The Fields object is now ready to be used with the viewer.
              parameterFields.add(paramField_1);
              parameterFields.add(paramField_2);
              parameterFields.add(paramField_3);
              
              viewer.setReportSource(reportSource);
              viewer.setOwnPage(true);
//              viewer.setOwnForm(true);
              viewer.setPrintMode(CrPrintMode.PDF);
              
              viewer.setParameterFields(parameterFields);
                  
            } catch (ReportSDKException e) {
                  e.printStackTrace();
                  
            } catch (ReportSDKExceptionBase e) {
                  e.printStackTrace();
                  
            }
            
      }
Nada SarhanJava EE developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mlmccCommented:
what is your question or problem?

mlmcc
0
Nada SarhanJava EE developerAuthor Commented:
I can't pass parameter to the crystal report.
0
mlmccCommented:
Does the report have parameters?

What happens when you run the code?
Do you get prompted for the values?

mlmcc
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Nada SarhanJava EE developerAuthor Commented:
The Log file give me this exception :
 javax.el.ELException: /index.xhtml @31,111 actionListener="#{manageBean.print(manageBean.list_all)}": java.lang.NullPointerException
      at com.sun.faces.facelets.el.TagMethodExpression.invoke(Unknown Source)
      at javax.faces.event.MethodExpressionActionListener.processAction(Unknown Source)
      at javax.faces.event.ActionEvent.processListener(Unknown Source)
      at javax.faces.component.UIComponentBase.broadcast(Unknown Source)
      at javax.faces.component.UICommand.broadcast(Unknown Source)
      at javax.faces.component.UIViewRoot.broadcastEvents(Unknown Source)
      at javax.faces.component.UIViewRoot.processApplication(Unknown Source)
      at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(Unknown Source)
      at com.sun.faces.lifecycle.Phase.doPhase(Unknown Source)
      at com.sun.faces.lifecycle.LifecycleImpl.execute(Unknown Source)
      at javax.faces.webapp.FacesServlet.service(Unknown Source)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
      at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
      at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:500)
      at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
      at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
      at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
      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)
Caused by: java.lang.NullPointerException
      at com.managebean.manageBean.print(manageBean.java:429)
      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:483)
      at org.apache.el.parser.AstValue.invoke(AstValue.java:247)
      at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
      ... 33 more
______________________________
but i think the exception coz something error in the method
0
Nada SarhanJava EE developerAuthor Commented:
Sorry, but I do some effect, but it not work, the value that pass to the report is index not the value here is the methods:
also the exception is disappear.

public void print(List<Child> list) {
            
            try {
                  
                  // Instantiate the report object
                  String report_Location;
                  report_Location = "/com/reports/Report1.rpt";
                  
                  // This interface provides a common service point for getting a report source that is used to view and export reports.
                  IReportSourceFactory2 rptSrcFactory = new JPEReportSourceFactory();
                  
                  // It is recommended to use the ReportClientDocument to obtain an IReportSource object.
                  // it's also Creates a new reportsource for the report specified.
                  Object reportSource = null;
                  reportSource = rptSrcFactory.createReportSource(report_Location, java.util.Locale.getDefault());
                  
//                  IReportSource reportSource = (IReportSource) rptSrcFactory.createReportSource(report_Location, java.util.Locale.getDefault());
                  
                  // creating a Fields object, populating it with the parameter values then adding that to the viewer.
//              CrystalReportViewer viewer = new CrystalReportViewer();
              
              /*
               * Before parameter fields can be set in a report, the fields must first be created and initialized.
               * Individual parameter fields are all stored in a Fields object.
               * The Fields object is simply a collection of different fields that can be passed to the viewer.
               */
              
              //---------- Create the Parameter Field Objects ----------//
              
              // Create a Fields object to store the parameter fields in.
              // Returns an object that contains the parameters in the report.
              Fields parameterFields = new Fields();
//              parameterFields = viewer.getParameterFields();
              
              // Clear previous parameter
//              parameterFields.clear();
              
              // Create a ParameterField object for each field that you wish to set.
              ParameterField paramField_1 = new ParameterField();
              ParameterField paramField_2 = new ParameterField();
              ParameterField paramField_3 = new ParameterField();
              
              // You must set the report name.
              // Set the report name to an empty string if your report does not contain a subreport; otherwise, the report name will be the name of the subreport
              /*
               * You also must set the parameter field’s report name to the report name associated with the parameter.
               * If the parameter field is for the main report, use a blank string ("").
               * The report name is set using the setReportName method.
               */
              paramField_1.setReportName("");
      
              // Create a Values object and a ParameterFieldDiscreteValue object for each parameter field you wish to set.
              Values value_1 = new Values();
              Values value_2 = new Values();
              Values value_3 = new Values();
              
              // If a ranged value is being set, a ParameterFieldRangeValue object should be used instead of the discrete value object.
              
              ParameterFieldDiscreteValue paramFieldDV_1 = new ParameterFieldDiscreteValue();
              ParameterFieldDiscreteValue paramFieldDV_2 = new ParameterFieldDiscreteValue();
              ParameterFieldDiscreteValue paramFieldDV_3 = new ParameterFieldDiscreteValue();
              
//              ParameterFieldRangeValue paramFieldDV_1 = new ParameterFieldRangeValue();
//              ParameterFieldRangeValue paramFieldDV_2 = new ParameterFieldRangeValue();
//              ParameterFieldRangeValue paramFieldDV_3 = new ParameterFieldRangeValue();
              
              //----------- Initialize the parameter fields -----------//
              
              // Set the name and value for each parameter field that is added.
              // Values for parameter fields are represented by a ParameterFieldDiscreteValue or ParameterFieldRangeValue object.
              // "paramField_1" ("id") is parameter name in my Crystal Report file (Report1.rpt)
              
              paramField_1.setName("@id");
              paramField_2.setName("@name");
              paramField_3.setName("@age");
              
              for(int i = 0; i < list_all.size(); i++) {
                    
                    paramFieldDV_1.setValue(list_all.get(i).getId());
                    paramFieldDV_2.setValue(list_all.get(i).getName());
                    paramFieldDV_3.setValue(list_all.get(i).getAge());
                   
                    System.out.println("");
                    System.out.println("paramFieldDV.setValue");
                    System.out.println("");
                    
                    // Add the parameter field values to the Values collection object.
                    value_1.add(paramFieldDV_1);
                    value_2.add(paramFieldDV_2);
                    value_3.add(paramFieldDV_3);
                   
                    System.out.println("");
                    System.out.println("value.add");
                    System.out.println("");
             
                    // Set the current Values collection for each parameter field.
                    paramField_1.setCurrentValues(value_1);
                    paramField_2.setCurrentValues(value_2);
                    paramField_3.setCurrentValues(value_3);
                   
                        // its give me index not the value.
                        
                    System.out.println(paramField_1.getCurrentValues());
                    System.out.println(paramField_2.getCurrentValues());
                    System.out.println(paramField_3.getCurrentValues());
             
                    // Add each parameter field to the Fields collection.
                    // The Fields object is now ready to be used with the viewer.
                    parameterFields.add(paramField_1);
                    parameterFields.add(paramField_2);
                    parameterFields.add(paramField_3);
                   
                    System.out.println("");
                    System.out.println("parameterFields.add");
                    System.out.println("");
                   
              }
              
                // create the viewer and render the report
                CrystalReportViewer viewer = new CrystalReportViewer();
                  
              viewer.setReportSource(reportSource);
              viewer.setOwnPage(true);
              viewer.setOwnForm(true);
                  
              // set the viewer print mode
              viewer.setPrintMode(CrPrintMode.ACTIVEX);
              viewer.setPrintMode(CrPrintMode.PDF);
                  
                  // set the parameters into the viewer
              viewer.setParameterFields(parameterFields);
              
              //---------- export pdf from crystal report ----------//
              
              String exportFileName = "E:\\pdf_1.pdf";
              
              ReportClientDocument clientDoc = new ReportClientDocument();
                  clientDoc.open(report_Location, ReportExportFormat._PDF);
              
                  //---------- Writing into external PDF file ----------//
                  
              //----- delete exist PDF file -----//
              
//                  try {
//                        Path fileToDeletePath = Paths.get("E:\\pdf_1.pdf");
//                        Files.delete(fileToDeletePath);
//                        
//                        System.out.println("step -> delete old pdf");
//                        
//                  } catch (IOException e1) {
//                        e1.printStackTrace();
//                  }
              
                  ByteArrayInputStream byteArrayInputStream = (ByteArrayInputStream) clientDoc.getPrintOutputController().export(ReportExportFormat.PDF);
                  int size = byteArrayInputStream.available();
                  byte[] barray = new byte[size];
                  
                  FileOutputStream fileOutputStream;
                  
                  try {
                        
                        fileOutputStream = new FileOutputStream(new File(exportFileName));

                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(size);
                        
                        int bytes = byteArrayInputStream.read(barray, 0, size);
                        
                        byteArrayOutputStream.write(barray, 0, bytes);
                        byteArrayOutputStream.writeTo(fileOutputStream);
                        
                        clientDoc.close();
                        byteArrayInputStream.close();
                        byteArrayOutputStream.close();
                        fileOutputStream.close();
                        
                  } catch (FileNotFoundException e) {
                        e.printStackTrace();
                        
                  } catch (IOException e) {
                        e.printStackTrace();
                  }
                  
            } catch (ReportSDKException e) {
                  e.printStackTrace();
                  
            } catch (ReportSDKExceptionBase e) {
                  e.printStackTrace();
                  
            }
            
            System.out.println("Finish print method");
            
      }
0
mlmccCommented:
Does the report have parameters?

 Do you get prompted for the values?

I assume it is erroring out.
Any idea which line is causing the error?

mlmcc
0
Nada SarhanJava EE developerAuthor Commented:
yes report have parameter, i don't have any idea where the error in this method.
0
mlmccCommented:
Should have asked earlier - What version of Crystal Reports?

mlmcc
0
Nada SarhanJava EE developerAuthor Commented:
crystal_reports_2016_sp04_51052106
0
mlmccCommented:
There is no SDK for the stand alone versions.  You need to use the version for  Visual Studio.

It is a free down load from SAP
https://www.sap.com/products/crystal-visual-studio.html

mlmcc
0
Nada SarhanJava EE developerAuthor Commented:
it's for .Net, not java ?
0
mlmccCommented:
That is the only SDK available.
SAP is working hard to get away from JAVA and use only HTML5

mlmcc
0
Nada SarhanJava EE developerAuthor Commented:
I solve this error by use different way, instead of passing parameter from java I make JDBC connection between crystal report and database.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSF

From novice to tech pro — start learning today.