title: Class not found when using SwingUtilities.invokeLater in OSGi Bundle
body: <p>(EDIT: Problem is solved - see details at the end)</p>
  
  <p>I want to create a Swing <code>JFrame</code> with a <code>WindowAdapter</code> in an OSGi Bundle. When I do this using <code>SwingUtilities.invokeLater</code>, the <code>WindowAdapter</code> class is not found. Without <code>invokeLater</code> it works. </p>
  
  <p>What do I need to do so that <code>WindowAdapter</code> is found when using <code>invokeLater</code>?
  Is <code>invokeLater</code> inappropriate in an OSGi environment?</p>
  
  <p>The details:</p>
  
  <p>I start an Apache Felix framework instance with my custom launcher, install the bundle and start it. My bundle's start method looks like this:</p>
  
  <pre><code>public void start(BundleContext arg0) throws Exception {
      myFrame = new MyFrame();
      myFrame.open();
  }
  </code></pre>
  
  <p>This is the MyFrame class:</p>
  
  <pre><code>public class MyFrame {
      JFrame mainFrame;
  
      public void open() {
          SwingUtilities.invokeLater(new Runnable() {
              @Override
              public void run() {
                  openImpl();
              }
          });
          // If called like this it works:
          // openImpl();
      }
  
      public void openImpl() {
          mainFrame = new JFrame("Title");
          mainFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
          WindowAdapter wa = new WindowAdapter() {
          };
          mainFrame.addWindowListener(wa);
          mainFrame.setSize(800, 600);
          mainFrame.setLocationRelativeTo(null);
          mainFrame.setVisible(true);
      }
  }
  </code></pre>
  
  <p>This is my manifest for the bundle:</p>
  
  <pre><code>Manifest-Version: 1.0
  Ant-Version: Apache Ant 1.8.2
  Created-By: 1.7.0_03-b05 (Oracle Corporation)
  Built-By: Rainer Schwarze
  Bundle-Name: DummyBdl
  Bundle-Description: Dummy Bundle
  Bundle-Vendor: admaDIC
  Bundle-Version: 0.0.1
  Bundle-Activator: dummybdl.Activator
  Import-Package: org.osgi.framework, javax.swing
  Export-Package: dummybdl.api
  Export-Service: dummybdl.Provider
  </code></pre>
  
  <p>And this is the stack trace which I get:</p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
      at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1432)
      at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:72)
      at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1843)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
      at dummybdl.MyFrame.openImpl(MyFrame.java:24)
      at dummybdl.MyFrame$1.run(MyFrame.java:16)
      at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
      at java.awt.EventQueue.access$000(EventQueue.java:101)
      at java.awt.EventQueue$3.run(EventQueue.java:666)
      at java.awt.EventQueue$3.run(EventQueue.java:664)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
  </code></pre>
  
  <p>Being an OSGi newbie I tried several things to fix it, but couldn't find a solution so far. So why not make my first question at StackOverflow :-)</p>
  
  <p>EDIT:</p>
  
  <p>After debugging for half an hour it turns out that the problem was entirely mine:
  My code stopped the OSGi framework before <code>openImpl</code> gets called in the EDT. </p>
  
  <p>So the Apache Felix framework marked the <code>BundleWiringImpl</code> instance (see stack trace) as disposed. When my <code>openImpl</code> gets called in the EDT, <code>BundleWiringImpl.getClassLoader</code> returns <code>null</code> because it is marked as disposed. Eventually this leads to the NPE.
  (I should have gone the extra steps of posting the 50 lines of my Felix loader which might have made the error obvious.)</p>