JDK-6179222 : Possible NPE with EventHandler
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.beans
  • Affected Version: 6
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux_redhat_9.0
  • CPU: x86
  • Submitted: 2004-10-14
  • Updated: 2010-04-02
  • Resolved: 2004-11-01
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 b11Fixed
Related Reports
Relates :  
Description
Here's the test case:
import java.awt.*;
import java.awt.event.ActionListener;
import java.beans.EventHandler;
import javax.swing.*;

public class EventHandlerBug {
     public static void main(String[] args) {
         Object target = new EventHandlerBug();
         JButton b = new JButton("hi");
         ActionListener l = (ActionListener)
EventHandler.create(ActionListener.class, target, "foo",
"source.icon");
         b.addActionListener(l);
         b.doClick();
     }

     public void foo(Icon o) {
         System.out.println("Icon: " + o);
     }
}

When run you get the following NPE:
Exception in thread "main" java.lang.NullPointerException
        at java.beans.EventHandler.invoke(EventHandler.java:354)
        at $Proxy0.actionPerformed(Unknown Source)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1\863)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.ja\va:2183)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonMode\l.java:420)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:25\8)
        at javax.swing.AbstractButton.doClick(AbstractButton.java:302)
        at javax.swing.AbstractButton.doClick(AbstractButton.java:282)
        at EventHandlerBug.main(EventHandlerBug.java:14)
###@###.### 10/14/04 16:24 GMT

Comments
EVALUATION The solution is pretty straightforward, in so far as just add a check for null. But there is a subtle problem, in that if the target has overloaded methods, eg: public void foo(String); public void foo(Icon); It's non-deterministic as to which method will be invoked. The reason that happens is that null will match either of these methods. It's possible we could try and figure out the return type based on the Method signature and look for the method based on that, but that isn't foolproof in so far as if the return type is typed to Object either method will match. Similarly you might not be able to resolve the method depending upon where null is returned. For those reasons we're going with the simple fix and will discourage people from targetting a class with overloaded methods. ###@###.### 10/14/04 17:35 GMT
14-10-2004

SUGGESTED FIX EventHandler should do: argTypes = new Class[]{input == null ? null : input.getClass()}; rather than: argTypes = new Class[] {input.getClass()}; ###@###.### 10/14/04 16:24 GMT
14-10-2004