1

I have an applet that visualizes a large graph using JUNG2. A while back I managed to add an export feature which allows the user to export the graphs to vector-based images, this time utilizing FreeHEP library. It all worked nice and smooth (I even used PDF exports in a publication).

Now the applet seems to crash when I try to export the graphs. Digging into the java console I found out that it's due a NPE deep down in the library. Below is the relevant portion of the stack trace:

Exception in thread "AWT-EventQueue-2" java.lang.ExceptionInInitializerError
    at org.freehep.graphicsio.gif.GIFImageWriteParam.<init>(GIFImageWriteParam.java:28)
    at org.freehep.graphicsio.gif.GIFImageWriter.getDefaultWriteParam(GIFImageWriter.java:73)
    at org.freehep.graphicsio.exportchooser.ImageExportFileType.<init>(ImageExportFileType.java:71)
    at org.freehep.graphicsio.exportchooser.ImageIOExportFileType.onRegistration(ImageIOExportFileType.java:50)
    at javax.imageio.spi.SubRegistry.registerServiceProvider(ServiceRegistry.java:715)
    at javax.imageio.spi.ServiceRegistry.registerServiceProvider(ServiceRegistry.java:302)
    at org.freehep.util.export.ExportFileTypeRegistry.addApplicationClasspathExportFileTypes(ExportFileTypeRegistry.java:122)
    at org.freehep.util.export.ExportFileTypeRegistry.getDefaultInstance(ExportFileTypeRegistry.java:50)
    at org.freehep.util.export.ExportFileType.getExportFileTypes(ExportFileType.java:181)
    at org.freehep.util.export.ExportFileType.getExportFileTypes(ExportFileType.java:173)
    at org.freehep.util.export.ExportDialog.addAllExportFileTypes(ExportDialog.java:64)
    at org.freehep.util.export.ExportDialog.<init>(ExportDialog.java:130)
    at org.freehep.util.export.ExportDialog.<init>(ExportDialog.java:90)
    at org.freehep.util.export.ExportDialog.<init>(ExportDialog.java:82)
    at myproject.visualization.ExtendedModelGraphMouse$ExportActionListener.actionPerformed(ExtendedModelGraphMouse.java:167)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    ...
Caused by: java.lang.NullPointerException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.freehep.graphics2d.PixelGraphics2D.<clinit>(PixelGraphics2D.java:101)
    ... 53 more

I have been digging into the source code of the FreeHEP library (hosted at Grep Code), the last line (topmost) in the stack trace points to the line that starts with UserProperties

public GIFImageWriteParam(Locale locale) {
    super(locale);
    canWriteProgressive = true;
    progressiveMode = MODE_DEFAULT;

    UserProperties def = new UserProperties(GIFGraphics2D
            .getDefaultProperties());
    quantizeColors = def.isProperty(GIFGraphics2D.QUANTIZE_COLORS);
    quantizeMode = def.getProperty(GIFGraphics2D.QUANTIZE_MODE);
}

From what I can decipher this should mean that GIFGraphics2D turns out to be null - why, I don't understand. I have double checked my build file to make sure necessary libs are in place, and neither my build file nor my code that utilizes the ExportDialog (see the very first link in the question body) has changed in over a year. So what could be the issue here?

Community
  • 1
  • 1
posdef
  • 6,498
  • 11
  • 46
  • 94

2 Answers2

2

Freehep is using an unpublished internal java SDK method. And the implementation of this method changed with java version 1.7. You probably upgraded your JDK/JRE to the latest java release.

Fortunately this has been fixed in the github repository of freehep (I could not find a freehep release after 2010, but it is being developed and available on github and maven).

The issue in github can be found here.

A bug-report with some information on the Oracle Site.

So update your freehep installation from github, rebuild it, and use the new libraries.

arjenve
  • 2,313
  • 1
  • 12
  • 7
1

I am a novice with github, Maven, etc. So please forgive me if the following is either trivial or unclear.

Despite using the github version of freehep-vectorgraphics (version 2.2.2), I still get this error message when running my code:

Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
at org.freehep.graphicsio.gif.GIFImageWriteParam.<init>(GIFImageWriteParam.java:28)
at org.freehep.graphicsio.gif.GIFImageWriter.getDefaultWriteParam(GIFImageWriter.java:73)
at org.freehep.graphicsio.exportchooser.ImageExportFileType.<init>(ImageExportFileType.java:71)
at org.freehep.graphicsio.gif.GIFExportFileType.<init>(GIFExportFileType.java:42)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

Line 28 of GIFImageWriteParam.java looks like this:

UserProperties def = new UserProperties(GIFGraphics2D.getDefaultProperties());

According to Mark Donszelmann on github, the error is due to a bug in PixelGraphics2D. The patch he suggested contains the variable 'clazz'. Here's the section from PixelGraphics2D where this variable appears. It looks very different from the text of the patch:

Class<?> clazz = Class.forName("sun.awt.X11GraphicsEnvironment");
displayX11 = true;
Method method = clazz.getMethod("isDisplayLocal");
if (Modifier.isStatic(method.getModifiers())) {
        // JDK 1.6
        displayLocal = (Boolean) method.invoke(null);
} else {
        try {
                // since JDK 1.7
                displayLocal = (Boolean)method.invoke(clazz.newInstance());
        } catch (InstantiationException  e) {
        }
}

Any idea how I can fix this problem?

janonime
  • 125
  • 5