JDK-6459089 : ArithmeticException with JEditorPane containing HTML
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6
  • Priority: P2
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2006-08-10
  • Updated: 2011-01-19
  • Resolved: 2006-08-23
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b97Fixed
Related Reports
Relates :  
Description
Compile and run the following test case. When it loads, hit PageUp or PageDown:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class EPTest extends JFrame {
    
    public EPTest() {
        super("EPTest");
        
        JEditorPane ep = new JEditorPane();
        ep.setContentType("text/html");
        ep.setText("<html><body>abc</body></html>");
        ep.setEditable(false);
        getContentPane().add(new JScrollPane(ep));
        ep.setCaretPosition(0);
    }

    private static void createAndShowGUI(String[] args) {
        EPTest test = new EPTest();
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        test.setSize(400, 400);
        test.setLocationRelativeTo(null);
        test.setVisible(true);
    }
    
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI(args);
            }
        });
    }
}

The following exception is printed:

Exception in thread "AWT-EventQueue-0" java.lang.ArithmeticException: / by zero
	at javax.swing.text.DefaultEditorKit$VerticalPageAction.actionPerformed(DefaultEditorKit.java:1414)
	at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1634)
	at javax.swing.JComponent.processKeyBinding(JComponent.java:2824)
	at javax.swing.JComponent.processKeyBindings(JComponent.java:2859)
	at javax.swing.JComponent.processKeyEvent(JComponent.java:2787)
	at java.awt.Component.processEvent(Component.java:5812)
	at java.awt.Container.processEvent(Container.java:2045)
	at java.awt.Component.dispatchEventImpl(Component.java:4407)
	at java.awt.Container.dispatchEventImpl(Container.java:2103)
	at java.awt.Component.dispatchEvent(Component.java:4237)
	at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
	at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:693)
	at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:952)
	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:824)
	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:657)
	at java.awt.Component.dispatchEventImpl(Component.java:4279)
	at java.awt.Container.dispatchEventImpl(Container.java:2103)
	at java.awt.Window.dispatchEventImpl(Window.java:2429)
	at java.awt.Component.dispatchEvent(Component.java:4237)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

Can also be reproduced with SwingSet2:

  Start SwingSet2, select an Internal Frame (just click at "Frame 0")
  then switch to the "Source Code" tab, and when the source is
  loaded, hit PageDown.

This is a regression introduced by the fix to 6244705.
http://javaweb.sfbay/jcg/1.6.0-mustang/swing/6244705/src/share/classes/javax/swing/text/DefaultEditorKit.java.sdiff.html

Previously, the code looked like:
yOffset = direction * (int)Math.ceil(scrollAmount / (double)h) * h; 

Now there is:
scrollAmount = direction * scrollAmount / h * h;

The problem appears to be the fact that "h" is 0 when the caret position is within the header of the document. The new code, when dividing by 0, throws an exception. Previously, however, we were using doubles. The result of dividing by 0 was "Infinity", which was then casted to the int value 2147483647.

It looks to me like both old and new code are wrong in their dealing with 0, but the previous code (at least) didn't throw an exception.

Comments
SUGGESTED FIX http://javaweb.sfbay/jcg/1.6.0-mustang/swing/6459089/
18-08-2006

EVALUATION We try to adjust scrollAmount so that it is a multiple of caret height. If caret height is zero, let's just skip the adjustment part.
16-08-2006