PDA

View Full Version : Null Pointer Exception when using TableModel



Anonymous
03-20-2002, 12:37 PM
Since I couldn't find an example of using JFreeReport with data from a TableModel I'm taking a guess on how to use it, but I keep getting the following error (see below). The code I'm using is:

private void doPrint() {
PreviewFrame frame1;
JFreeReport jfr = new JFreeReport("Test Report");

jfr.setData(tableModel);

frame1 = new PreviewFrame(jfr, 640, 400);

frame1.pack();
frame1.setLocation(100,100);
frame1.show();
}


Any ideas?

Exception occurred during event dispatching:
java.lang.NullPointerException
at com.jrefinery.report.JFreeReport.advanceState(Unknown Source)
at com.jrefinery.report.JFreeReport.processPage(Unknown Source)
at com.jrefinery.report.ReportPane.repaginate(Unknown Source)
at com.jrefinery.report.ReportPane.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(JComponent.java:739)
at javax.swing.JComponent.paintChildren(JComponent.java:523)
at javax.swing.JComponent.paint(JComponent.java:748)
at javax.swing.JComponent.paintChildren(JComponent.java:523)
at javax.swing.JComponent.paint(JComponent.java:748)
at javax.swing.JViewport.paint(JViewport.java:668)
at javax.swing.JComponent.paintChildren(JComponent.java:523)
at javax.swing.JComponent.paint(JComponent.java:748)
at javax.swing.JComponent.paintChildren(JComponent.java:523)
at javax.swing.JComponent.paint(JComponent.java:748)
at javax.swing.JComponent.paintChildren(JComponent.java:523)
at javax.swing.JComponent.paint(JComponent.java:748)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:546)
at javax.swing.JComponent.paintChildren(JComponent.java:523)
at javax.swing.JComponent.paint(JComponent.java:719)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:23)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:54)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:91)
at java.awt.Container.paint(Container.java:960)
at sun.awt.RepaintArea.paint(RepaintArea.java:298)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:193)
at java.awt.Component.dispatchEventImpl(Component.java:2665)
at java.awt.Container.dispatchEventImpl(Container.java:1213)
at java.awt.Window.dispatchEventImpl(Window.java:912)
at java.awt.Component.dispatchEvent(Component.java:2499)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:319)
at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java:103)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:84)

Anonymous
03-20-2002, 01:42 PM
I haven't looked at the source a lot, but it appears in order to have a valid report, the pieces of the rpeort must be built before calling previewFrame. Look at the demo app for help. It uses two models which implement TableModel (I have used my own TableModels without problems).

The key to building the report appears to be reading the XML file with the report definitions in it and using the XML handler to create the report object. The following code demonstrates this:

// read in the xml file and use the content handler to parse
// the file and generate a JFreeReport
File file = new File("testreport.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
ReportDefinitionContentHandler handler = new ReportDefinitionContentHandler();

InputStream in = new FileInputStream(file);
parser.parse(in, handler);
JFreeReport report = handler.getReport();

// not set the data with any table model
report.setData(model);

// now preview the report
frame = new PreviewFrame(report, 640, 400);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// assumes frame is an instance var
frame.dispose();
frame = null;
}
});
frame.pack();
frame.setLocation(100,100);
frame.show();


HTH,

Steve

Anonymous
03-20-2002, 01:52 PM
I promise that I am writing some documentation for how to create your own reports, but I only have a limited amount of time to work on it.

The problem with your code is that the JFreeReport constructor just creates an empty report with null headers, footers and item bands. The generator should fail a bit more gracefully of course, so I'll work on that...

What you need to do is create an XML report definition file - the format isn't documented yet, so you'll have to reverse engineer the two examples which you will find in the jars directory of the download.

Then copy the code in the demo application that constructs a fully populated JFreeReport object from the XML report definition file...and then you should see some output. Make sure the field names you use in the XML file match the column names in your table model.

Ask me when you have more questions...

Regards,

DG.

Anonymous
03-20-2002, 01:54 PM
Hi Steve,

Just noticed you already responded to the question...thanks for that, I appreciate it!

Regards,

DG.