Bug 42381

Summary: NullPointerException in JSVGScrollPane.getViewBoxRect when loading svg document with no viewBox attribute
Product: Batik - Now in Jira Reporter: Jeff Grigg <Jeff.Grigg>
Component: SVG ViewerAssignee: Batik Developer's Mailing list <batik-dev>
Status: RESOLVED FIXED    
Severity: normal CC: Jeff.Grigg
Priority: P4 Keywords: ErrorMessage
Version: 1.7   
Target Milestone: ---   
Hardware: PC   
OS: Linux   

Description Jeff Grigg 2007-05-10 08:24:29 UTC
When loading a document with no 'viewBox' attribute in the header, the last line
of the JSVGScrollPane.getViewBoxRect method throws a NullPointerException.  This
happens because the GraphicsNode (gn) is present (not null), but 'getBounds()'
returns null, so 'gn.getBounds().clone()' on the last line of the method throws
NullPointerException.

Suggested fix:
at the end of the JSVGScrollPane.getViewBoxRect method,
change
        return (Rectangle2D)gn.getBounds().clone();
to
        Rectangle2D bounds = gn.getBounds();
        if (bounds == null) return null;

        return (Rectangle2D) bounds.clone();
[end patch]

Impact of bug:  The bug is annoying but not severe: 
EventDispatcher.dispatchEvent catches the exception (as Throwable) and does
'.printStackTrace()', sending it to System.err.  It's annoying and causes people
to get nervous, to see a NullPointerException in the output/log every time an
image is loaded.  But other than that, the effect of the exception is about the
same as more careful null checking and returning null, as is done in most other
cases in the JSVGScrollPane.getViewBoxRect method:  Returning null pretty much
causes the callers to avoid doing any further processing, and the exception
accomplishes the same result, less gracefully.   ;->

Has been observed in both Microsoft Windows and Linux environments.


Example problem document:
<?xml version="1.0" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" id="body" width="2048" height="1536" ></svg>

Example good document:
<?xml version="1.0" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" id="body" width="2048" height="1536"
viewBox="0 0 800 600" ></svg>


Stack trace:
java.lang.NullPointerException
	at org.apache.batik.swing.JSVGScrollPane.getViewBoxRect(JSVGScrollPane.java:607)
	at
org.apache.batik.swing.JSVGScrollPane.checkAndSetViewBoxRect(JSVGScrollPane.java:584)
	at org.apache.batik.swing.JSVGScrollPane.resizeScrollBars(JSVGScrollPane.java:451)
	at
org.apache.batik.swing.JSVGScrollPane$ScrollListener.gvtRenderingCompleted(JSVGScrollPane.java:391)
	at org.apache.batik.swing.gvt.GVTTreeRenderer$4.dispatch(GVTTreeRenderer.java:193)
	at org.apache.batik.util.EventDispatcher.dispatchEvent(EventDispatcher.java:103)
	at org.apache.batik.util.EventDispatcher.fireEvent(EventDispatcher.java:87)
	at org.apache.batik.util.EventDispatcher$1.run(EventDispatcher.java:46)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:199)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
	at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
	at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Comment 1 Cameron McCormack 2007-09-26 20:56:21 UTC
Yep agreed, thanks!  Fixed in SVN now.