Estoy aprendiendo a hacer reportes y lo estoy aplicando a una Aplicación de Gestión con Hibernate que estoy realizando.He seguido varios tutoriales pero me da un error en el JRLoader.
Primero cree una clase java para los reportes:
public class ReportePresupuesto {private int presupuestoId;private String presupuestoNombre;private String nombreCliente;private String dniCifCliente;private String direccionCliente;private int telefonoCliente;private String concepto;private double precio;private int cantidad;private double subtotal;private double total;private double iva;private double totalIva;public ReportePresupuesto() {}public ReportePresupuesto(int presupuestoId, String presupuestoNombre, String nombreCliente, String dniCifCliente, String direccionCliente, int telefonoCliente, String concepto, double precio, int cantidad, double subtotal, double total, double iva, double totalIva) { this.presupuestoId = presupuestoId; this.presupuestoNombre = presupuestoNombre; this.nombreCliente = nombreCliente; this.dniCifCliente = dniCifCliente; this.direccionCliente = direccionCliente; this.telefonoCliente = telefonoCliente; this.concepto = concepto; this.precio = precio; this.cantidad = cantidad; this.subtotal = subtotal; this.total = total; this.iva = iva; this.totalIva = totalIva;} public ReportePresupuesto(int presupuestoId, String presupuestoNombre, String nombreCliente, String dniCifCliente, String direccionCliente, int telefonoCliente, double total, double iva, double totalIva) { this.presupuestoId = presupuestoId; this.presupuestoNombre = presupuestoNombre; this.nombreCliente = nombreCliente; this.dniCifCliente = dniCifCliente; this.direccionCliente = direccionCliente; this.telefonoCliente = telefonoCliente; this.total = total; this.iva = iva; this.totalIva = totalIva;} public ReportePresupuesto(String concepto, double precio, int cantidad, double subtotal) { this.concepto = concepto; this.precio = precio; this.cantidad = cantidad; this.subtotal = subtotal;}public int getPresupuestoId() { return presupuestoId;}public void setPresupuestoId(int presupuestoId) { this.presupuestoId = presupuestoId;}public String getPresupuestoNombre() { return presupuestoNombre;}public void setPresupuestoNombre(String presupuestoNombre) { this.presupuestoNombre = presupuestoNombre;}public String getNombreCliente() { return nombreCliente;}public void setNombreCliente(String nombreCliente) { this.nombreCliente = nombreCliente;}public String getDniCifCliente() { return dniCifCliente;}public void setDniCifCliente(String dniCifCliente) { this.dniCifCliente = dniCifCliente;}public String getDireccionCliente() { return direccionCliente;}public void setDireccionCliente(String direccionCliente) { this.direccionCliente = direccionCliente;}public int getTelefonoCliente() { return telefonoCliente;}public void setTelefonoCliente(int telefonoCliente) { this.telefonoCliente = telefonoCliente;}public String getConcepto() { return concepto;}public void setConcepto(String concepto) { this.concepto = concepto;}public double getPrecio() { return precio;}public void setPrecio(double precio) { this.precio = precio;}public int getCantidad() { return cantidad;}public void setCantidad(int cantidad) { this.cantidad = cantidad;}public double getSubtotal() { return subtotal;}public void setSubtotal(double subtotal) { this.subtotal = subtotal;}public double getTotal() { return total;}public void setTotal(double total) { this.total = total;}public double getIva() { return iva;}public void setIva(double iva) { this.iva = iva;}public double getTotalIva() { return totalIva;}public void setTotalIva(double totalIva) { this.totalIva = totalIva;}
Generé varios constructores: algunos piden los datos que formarán la tabla del reporte (como el concepto, la cantidad, el precio y los subtotales)y otros los datos que se introducirán solo una vez (como el nombre del cliente o el número del presupuesto)
Después utilice el nombre de cada atributo de la clase java de reportes para crear los Fields del Reporte.jrxml y lo compilé en el Reporte.jasper
Dentro de mi código del Jframe en el botón imprimir introduje el siguiente código:
List lista = new ArrayList(); String clienteNombre = (String) cmb_cre_pre_cli.getSelectedItem(); String concepto = txt_cre_pre_con.getText(); OperarClientes opeCli = new OperarClientes(); OperarLineaPresMaterial opeLinMat = new OperarLineaPresMaterial(); OperarLineaPresTrabajo opeLinTra = new OperarLineaPresTrabajo(); OperarPresupuestos opePre = new OperarPresupuestos(); List<Cliente> clientes = opeCli.buscarClientePorNombre(clienteNombre); Cliente cliente = new Cliente(); for(int x=0;x<clientes.size();x++){ cliente.setClienteId(clientes.get(x).getClienteId()); cliente.setClienteNombre(clientes.get(x).getClienteNombre()); cliente.setDniCif(clientes.get(x).getDniCif()); cliente.setClienteDireccion(clientes.get(x).getClienteDireccion()); cliente.setClienteTelefono(clientes.get(x).getClienteTelefono()); } int clienteId = opeCli.buscarClienteIdPorNombre(clienteNombre); List<Presupuesto> presupuestos = opePre.buscarPresupuesto(clienteId, concepto); Presupuesto presupuesto = new Presupuesto(); for(int x=0; x<presupuestos.size();x++){ presupuesto.setPresupuestoId(presupuestos.get(x).getPresupuestoId()); presupuesto.setCliente(presupuestos.get(x).getCliente()); presupuesto.setPresupuestoTrabajo(presupuestos.get(x).getPresupuestoTrabajo()); presupuesto.setPresupuestoTotal(presupuestos.get(x).getPresupuestoTotal()); } double total = presupuesto.getPresupuestoTotal(); double iva = (total*21)/100; double totalIva = total + iva; ReportePresupuesto reporte1 = new ReportePresupuesto(presupuesto.getPresupuestoId(), presupuesto.getPresupuestoTrabajo(), cliente.getClienteNombre(), cliente.getDniCif(), cliente.getClienteDireccion(),cliente.getClienteTelefono(), total, iva, totalIva); lista.add(reporte1); List<LineaPresMaterial> lineasMat = opeLinMat.buscarLinea(presupuesto); for(Object o : lineasMat){ LineaPresMaterial linea = (LineaPresMaterial) o; ReportePresupuesto reporte = new ReportePresupuesto(linea.getConcepto(),linea.getPrecio(), linea.getCantidad(), linea.getSubtotal()); lista.add(reporte); } List<LineaPresTrabajo> lineasTra = opeLinTra.buscarLinea(presupuesto); for(Object o : lineasTra){ LineaPresTrabajo linea = (LineaPresTrabajo) o; ReportePresupuesto reporte = new ReportePresupuesto(linea.getConcepto(),linea.getPrecio(), linea.getCantidad(), linea.getTotalLinea()); lista.add(reporte); } try{ JasperReport reporte = (JasperReport) JRLoader.loadObject(ClassLoader.getSystemResource("src\\Reportes\\ReportePresupuesto.jasper")); JasperPrint print = JasperFillManager.fillReport(reporte, null, new JRBeanCollectionDataSource(lista)); JasperViewer view = new JasperViewer(print, false); view.setVisible(true); view.setTitle("Presupuesto:"); }catch(JRException e){ e.printStackTrace(); }
Con la aplicación en funcionamiento al pulsar el botón Imprimir, me genera el siguiente error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerExceptionat net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:183)at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:167)at Vista.Presupuestos.CrearPresupuesto.btm_cre_pre_impActionPerformed(CrearPresupuesto.java:609)at Vista.Presupuestos.CrearPresupuesto.access$200(CrearPresupuesto.java:44)at Vista.Presupuestos.CrearPresupuesto$3.actionPerformed(CrearPresupuesto.java:164)at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)at java.awt.Component.processMouseEvent(Component.java:6533)at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)at java.awt.Component.processEvent(Component.java:6298)at java.awt.Container.processEvent(Container.java:2236)at java.awt.Component.dispatchEventImpl(Component.java:4889)at java.awt.Container.dispatchEventImpl(Container.java:2294)at java.awt.Component.dispatchEvent(Component.java:4711)at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)at java.awt.Container.dispatchEventImpl(Container.java:2280)at java.awt.Window.dispatchEventImpl(Window.java:2746)at java.awt.Component.dispatchEvent(Component.java:4711)at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)at java.awt.EventQueue.access$500(EventQueue.java:97)at java.awt.EventQueue$3.run(EventQueue.java:709)at java.awt.EventQueue$3.run(EventQueue.java:703)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)at java.awt.EventQueue$4.run(EventQueue.java:731)at java.awt.EventQueue$4.run(EventQueue.java:729)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Os adjunto además una captura de las librerías que he incluido para usar Jasper Report(además de plugin de iReport)
Sin nada más, muchas gracias de antemano y a ver si pudierais ayudarme.