Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4294758

BoxLayout causes java.lang.NullPointerException and applet crashes

XMLWordPrintable

    • 03
    • x86, sparc
    • linux, solaris_8, windows_98
    • Verified



        Name: skT88420 Date: 11/25/99


        java version "1.2.2"
        Classic VM (build JDK-1.2.2-001, native threads, symcjit)


        1) Run the applet using Appletviewer many times before you see a
        NullPointerException. You may have to run it 10-20 times.

        3) java.lang.NullPointerException:
                at javax.swing.BoxLayout.checkRequests(BoxLayout.java:362)
                at javax.swing.BoxLayout.layoutContainer(BoxLayout.java:296)
                at java.awt.Container.layout(Container.java:494)
                at java.awt.Container.doLayout(Container.java:484)
                at java.awt.Container.validateTree(Container.java:553)
                at java.awt.Container.validateTree(Container.java:560)
                at java.awt.Container.validateTree(Container.java:560)
                at java.awt.Container.validateTree(Container.java:560)
                at java.awt.Container.validateTree(Container.java:560)
                at java.awt.Container.validateTree(Container.java:560)
                at java.awt.Container.validateTree(Container.java:560)
                at java.awt.Container.validateTree(Container.java:560)
                at java.awt.Container.validate(Container.java:535)
                at sun.applet.AppletPanel.run(AppletPanel.java:336)
                at java.lang.Thread.run(Thread.java:479)

        5) This problem even appears on windows95 but less frequently

        2) html code for applet
        <html>
        <head>
        <title>Swing Applet</title>
        </head>

        <body>
        <APPLET code="StreamPPApplet.class" width=600 height=500> </APPLET>
        </body>
        </html>

        2) Java Code for the applet. I couldn't reproduce with a smaller code

        Code attached to the bug report.

        (Review ID: 98298)
        ======================================================================

        This bug renders the SAP GUI for Java more or less useless for end users.
        The box layout is used over and over. Not using it is no option.
        Please help. Bugs shows up as well in JDK 1.2.2_06 production release

        Stefan Schneider, MDE

        stefan.schneider@eng 2000-09-21


        The problem arises from the class BoxLayout.
        Some member variables are not MT safe and
        they are being used (to be more precise misused)
        in different threads at a time.

        The problem occures in JDK 1.1.8 1.2.2 and 1.3
        on all platforms.

        The relevant implementation of the class as is
        involves the following member variables
        xChildren, yChildren

        and the methods invalidateLayout and checkRequests.

        The crashes may happen for the following reason:
        invalidateLayout() may be called from a different thread
        while checkRequests() is processed by a another thread.

        The method invalidateLayout() is fairly robust since the
        only thing is does is setting xChildren and yChildren to null.
        This behaviour is safe and stable but fatal for the concurrent
        thread checkRequests()

        checkRequest tries to be smart and checks if xChildren and yChildren
        may been set to null ( see line 2).
        It then recreates the objects in line 6 and 7 .
        The troubles start behind line 7 since checkRequest()
        is naive enough (von Neumann machine) to assume the the variables
        are correctly set. Therefore is uses them without additional checks
        for the rest of the method.
        It is not aware that invalidateLayout() may interfere through a
        different
        thread and reset the 2 variables...

        Please continue reading below the reference code in order to check
        for 2 proposed solutions.

        /*
         * @(#)BoxLayout.java 1.24 00/02/02
         ...
         */
         
        package javax.swing;

        public class BoxLayout implements LayoutManager2, Serializable {

        private transient SizeRequirements[] xChildren;
        private transient SizeRequirements[] yChildren;
        ...

            /**
             * Indicates that a child has changed its layout related
        information,
             * and thus any cached calculations should be flushed.
             *
             * @param target the affected container
             *
             * @exception AWTError if the target isn't the container
                specified to the
             * BoxLayout constructor
             */
            public void invalidateLayout(Container target) {
                checkContainer(target);
                xChildren = null;
                yChildren = null;
                xTotal = null;
                yTotal = null;
            }
        ...

        1. void checkRequests() {
        2. if (xChildren == null || yChildren == null) {
        3. // The requests have been invalidated... recalculate
        4. // the request information.
        5. int n = target.getComponentCount();
        6. xChildren = new SizeRequirements[n];
        7. yChildren = new SizeRequirements[n];
        8. for (int i = 0; i < n; i++) {
        9. Component c = target.getComponent(i);
        10. if (!c.isVisible()) {
        11. xChildren[i] = new SizeRequirements(0,0,0,
        12. c.getAlignmentX());
        13. yChildren[i] = new SizeRequirements(0,0,0,
        14. c.getAlignmentY());
        15. continue;
        16. }
        17. Dimension min = c.getMinimumSize();
        18. Dimension typ = c.getPreferredSize();
        19 Dimension max = c.getMaximumSize();
        20. xChildren[i] = new SizeRequirements(min.width,
        typ.width,
        21. max.width,
        22.
        c.getAlignmentX());
        23. yChildren[i] = new SizeRequirements(min.height,
        typ.height,
        24. max.height,
        25.
        c.getAlignmentY());
        26. }
                    
                    if (axis == X_AXIS) {
                        xTotal =
        SizeRequirements.getTiledSizeRequirements(xChildren);
                        yTotal =
        SizeRequirements.getAlignedSizeRequirements(yChildren);
                    } else {
                        xTotal =
        SizeRequirements.getAlignedSizeRequirements(xChildren);
                        yTotal =
        SizeRequirements.getTiledSizeRequirements(yChildren);
                    }
                }
            }
                    
            private int axis;
            private Container target;

            private transient SizeRequirements[] xChildren;
            private transient SizeRequirements[] yChildren;
            private transient SizeRequirements xTotal;
            private transient SizeRequirements yTotal;
            
            private transient PrintStream dbg;
        }
        stefan.schneider@eng 2000-11-07

        abhijit.saha@Eng 2001-07-03

        updating the bugtraq for bugs integrated in appropriate version of jdk.

              tprinzing Tim Prinzing
              skonchad Sandeep Konchady
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: