Hi thanks for reading this post. Hope you could help me. I have this question i can't figure out. Thanks in advanced for helping :)

I kept receiving message dialog box containing "The document has no pages." whenever i click the PrintAllbutton.

I used jasperreports, in which i'm not familiar with, in netbeans 8.0 with mySql as my database.

Here is my code:

package newscreen;

import javax.swing.JFrame;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.design.JRDesignQuery;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;

public class PrintReport1 extends javax.swing.JFrame {

    Connection conn=null;

    public PrintReport1() {
        initComponents();
    }


    @SuppressWarnings("unchecked")

    private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
        conn= Connect.ConnectDB();
    }                                 

    private void btnPrintAllMouseClicked(java.awt.event.MouseEvent evt) {                                         
        JasperPrint jasperPrint;
        HashMap hm = null;
        Connection connect = null;
        setExtendedState(MAXIMIZED_BOTH);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        try {
            Class.forName("com.mysql.jdbc.Driver");
            // setup the connection with the DB.
            connect = DriverManager
            .getConnection("jdbc:mysql://localhost/order_and_purchase_system"
              + "user=root&password=");
        } catch (Exception ex) {
            System.out.println("Database.getConnection() Error -->" + ex.getMessage());            
        }


            /**You can also test this line if you want to display 
           * report from any absolute path other than the project root path*/

            String ReportFile = "C:/Users/PrincessJoy/Documents/Avatar/newlogscreen/report1.jrxml";
            String jasperFileName = "C:/Users/PrincessJoy/Documents/Avatar/newlogscreen/report1.jasper";

           InputStream inputx = null;   
        try {
            inputx = new FileInputStream(new File("C:/Users/PrincessJoy/Documents/Avatar/newlogscreen/report1.jrxml"));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PrintReport1.class.getName()).log(Level.SEVERE, null, ex);
        }

           try {    
            JasperDesign jasperDesign = JRXmlLoader.load(inputx);                   

            JRDesignQuery jrd = new JRDesignQuery();


            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);            
            jasperPrint = JasperFillManager.fillReport(jasperReport, null, connect);

            JasperViewer jv = new JasperViewer(jasperPrint);
            jv.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            jv.setExtendedState(MAXIMIZED_BOTH);
            jv.setVisible(true);

        } catch (JRException e) {
            e.printStackTrace();
        }               
    }                                        

    public static void main(String args[]) {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PrintReport1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PrintReport1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PrintReport1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PrintReport1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PrintReport1().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btnPrintAll;
    // End of variables declaration                   

    private JRDesignQuery JRDesignQuery() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

The java code seems correct, check the SQL query in your jrxml file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.