NPE inside MViewPane

User e21f251256

13-04-2007 18:03:03

When we add a component that contains a MViewPane to a JTabbedPane, we get a NullPointerException inside the MViewPane code. This is using JChem 3.2.4_01 on Java 1.5.0_08, on Windows XP.





java.lang.NullPointerException


at chemaxon.marvin.beans.MViewPane.setEnabled(MViewPane.java:1226)


at chemaxon.marvin.beans.MarvinPane.componentShown(MarvinPane.java:259)


at chemaxon.marvin.beans.MarvinPane.ancestorChange(MarvinPane.java:229)


at chemaxon.marvin.beans.MarvinPane.propertyChange(MarvinPane.java:207)


at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:333)


at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:270)


at java.awt.Component.firePropertyChange(Component.java:7159)


at javax.swing.JComponent.addNotify(JComponent.java:4470)


at java.awt.Container.addNotify(Container.java:2500)


at javax.swing.JComponent.addNotify(JComponent.java:4469)


at java.awt.Container.addNotify(Container.java:2500)


at javax.swing.JComponent.addNotify(JComponent.java:4469)


at javax.swing.JRootPane.addNotify(JRootPane.java:680)


at java.awt.Container.addNotify(Container.java:2500)


at javax.swing.JComponent.addNotify(JComponent.java:4469)


at java.awt.Container.addNotify(Container.java:2500)


at javax.swing.JComponent.addNotify(JComponent.java:4469)


at java.awt.Container.addNotify(Container.java:2500)


at javax.swing.JComponent.addNotify(JComponent.java:4469)


at java.awt.Container.addNotify(Container.java:2500)


at javax.swing.JComponent.addNotify(JComponent.java:4469)


at java.awt.Container.addNotify(Container.java:2500)


at javax.swing.JComponent.addNotify(JComponent.java:4469)


at java.awt.Container.addNotify(Container.java:2500)


at javax.swing.JComponent.addNotify(JComponent.java:4469)


at java.awt.Container.addImpl(Container.java:1052)


at javax.swing.JTabbedPane.insertTab(JTabbedPane.java:590)


at javax.swing.JTabbedPane.addTab(JTabbedPane.java:662)


(our code, adding component to JTabbedPane)


at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)


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)

ChemAxon 7c2d26e5cf

17-04-2007 09:23:05

Can you show me an example?


I had not managed to reproduce the error.


Here is my example (to test viewer in JTabbedPane).

User e21f251256

17-04-2007 15:35:16

An example will be hard to produce because our GUI is created in a complicated way. Can you tell me what is happening at the line MViewPane.java:1226 (where the Exception is coming from)? That might give a clue to what the problem is.

ChemAxon 7c2d26e5cf

17-04-2007 15:59:31

I don't know exactly to which JChem release do you refer.


Probably you talked about the first pre-release of 3.2.4 (3_2_4pre1).


In this version,the code is the following (MViewPane.java):


Code:
        if(menubar != null) {


            menubar.setEnabled(enabled);


            for(int i=0;i<menubar.getMenuCount();i++) {


                menubar.getMenu(i).setEnabled(enabled); // <-- line #1226


            }


        }


    }



It throws exception if the getMenu(i) returns with null. Normally, it should not be null. Probably, it is a synchronization problem at menu creation. Are you sure that the MViewPane's initalization and MViewPane.setVisible is called on the AWT thread?


Have you customized the menubar? I should know more about your code to debug it. If you want, we can continue this discussion in private.

User e21f251256

19-04-2007 19:25:20

Hi, I would be interested in continuing the conversation in private. How can I contact you?

ChemAxon 7c2d26e5cf

20-04-2007 12:44:43

I am writing you an email.


Tamas

ChemAxon 7c2d26e5cf

24-04-2007 15:17:31

Hi Ryan,


Thanks for your help in debugging.


This simple example (that you show me) helped me to reproduce the NullPointerException issue.


http://www.java2s.com/Code/Java/Swing-JFC/MenuSample3.htm


Code:
bar.add(Box.createHorizontalGlue());



You are right: the key was the horizontal glue in the example.


If a horizontal glue is added to a menu bar, JMenuBar.getMenu(i) will return null by this item.


I've fixed the code in Marvin to avoid NullPointerException in this case.





Tamas