United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6179222 : Possible NPE with EventHandler

Details
Type:
Bug
Submit Date:
2004-10-14
Status:
Resolved
Updated Date:
2010-04-02
Project Name:
JDK
Resolved Date:
2004-11-01
Component:
client-libs
OS:
linux_redhat_9.0
Sub-Component:
java.beans
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports
Relates:

Sub Tasks

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
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
                                     
2004-10-14
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
                                     
2004-10-14



Hardware and Software, Engineered to Work Together