/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package uniformacaocadastro;
/**
*
* @author tiago
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.view.*;
/**
*
* @author Edson
*/
public class Relatorio {
private Connection conn;
public Relatorio() throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
String db = "jdbc:mysql://localhost:3306/uniformacao";
conn = DriverManager.getConnection(db, "root", "123");
} catch (Exception e) {
throw new Exception("Erro: \n" + e.getMessage());
}
}
public void visualizar() {
try {
//caminho relativo do relatório dentro do pacote
String a = "relatorio/report2.jasper";
//lê o arquivo dentro do pacote
//mecessário caso gere um JAR para distribuir
InputStream in = this.getClass().getResourceAsStream(a);
//chama fillReport
JasperPrint jp = JasperFillManager.fillReport(in, new HashMap() , conn);
//exibe o relatório com viewReport
JasperViewer.viewReport(jp, false);
} catch (JRException e) {
e.printStackTrace();
} finally {
try {
if (!conn.isClosed()) {
conn.close();
}
} catch (SQLException ex) {
}
}
}
}
JasperPrint jp = JasperFillManager.fillReport(in, new HashMap() , conn);
A saída do netbeans é esta:
java.lang.NullPointerException
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2266)
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2279)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2750)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:780)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream.<init>(ContextClassLoaderObjectInputStream.java:53)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:193)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:376)
at uniformacaocadastro.Relatorio.visualizar(Relatorio.java:52)
at uniformacaocadastro.Principal.jMenuItem4ActionPerformed(Principal.java:253)
at uniformacaocadastro.Principal.access$700(Principal.java:21)
at uniformacaocadastro.Principal$8.actionPerformed(Principal.java:169)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
at javax.swing.AbstractButton.doClick(AbstractButton.java:337)
at javax.swing.plaf.basic.BasicMenuItemUI$Actions.actionPerformed(BasicMenuItemUI.java:1194)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1636)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2851)
at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(JMenuBar.java:670)
at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(JMenuBar.java:678)
at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(JMenuBar.java:678)
at javax.swing.JMenuBar.processKeyBinding(JMenuBar.java:649)
at javax.swing.KeyboardManager.fireBinding(KeyboardManager.java:267)
at javax.swing.KeyboardManager.fireKeyboardAction(KeyboardManager.java:254)
at javax.swing.JComponent.processKeyBindingsForAllComponents(JComponent.java:2928)
at javax.swing.SwingUtilities.processKeyBindings(SwingUtilities.java:1551)
at javax.swing.UIManager$2.postProcessKeyEvent(UIManager.java:1465)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:714)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:969)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:841)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:668)
at java.awt.Component.dispatchEventImpl(Component.java:4502)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Alguem pode me ajudar?