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

Null Pointer adding VetoableChangeListenerProxy to VetoableChangeSupport

XMLWordPrintable

    • b03
    • x86
    • windows_2000
    • Verified

      Name: gm110360 Date: 02/13/2004


      FULL PRODUCT VERSION :
      java version "1.4.1_02"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_02-b06)
      Java HotSpot(TM) Client VM (build 1.4.1_02-b06, mixed mode)

      FULL OS VERSION :
      Microsoft Windows 2000 [Version 5.00.2195]

      A DESCRIPTION OF THE PROBLEM :
      Here is a comparison of the source code for VetoableChangeSupport and PropertyChangeSupport

      First PropertyChangeSupport
      /**
           * Add a PropertyChangeListener to the listener list.
           * The listener is registered for all properties.
           *
           * @param listener The PropertyChangeListener to be added
           */
          public synchronized void addPropertyChangeListener(
      PropertyChangeListener listener) {

      if (listener instanceof PropertyChangeListenerProxy) {
      PropertyChangeListenerProxy proxy =
                         (PropertyChangeListenerProxy)listener;
      // Call two argument add method.
      addPropertyChangeListener(proxy.getPropertyName(),
                          (PropertyChangeListener)proxy.getListener());
      } else {
      if (listeners == null) {
      listeners = new java.util.Vector();
      }
      listeners.addElement(listener);
      }
          }
      Now VetoableChangeSupport
      /**
           * Add a VetoableListener to the listener list.
           * The listener is registered for all properties.
           *
           * @param listener The VetoableChangeListener to be added
           */

          public synchronized void addVetoableChangeListener(
      VetoableChangeListener listener) {
              if (listener instanceof VetoableChangeListenerProxy) {
                  VetoableChangeListenerProxy proxy =
                          (VetoableChangeListenerProxy)listener;
                  // Call two argument add method.
                  addVetoableChangeListener(proxy.getPropertyName(),
                          (VetoableChangeListener)proxy.getListener());
              } else {
                  if (listeners == null) {
                      listeners = new java.util.Vector();
                  }
              }
              listeners.addElement(listener);
          }

      Notice the different location of listeners.addElement(listener);
      In the veto version, listeners has not been initialised. The first time you try to add a VetoableChangeListenerProxy, listeners is null, therefore a NullPointerException is caused when trying to addElement

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      initialise a VetoableChangeSupport object and add a ListenerProxy as above

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected result is to be the same as adding PropertyChangeListenerProxy to PropertyChangeSupport
      ACTUAL -
      NullPointerException

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.lang.NullPointerException
              at java.beans.VetoableChangeSupport.addVetoableChangeListener(VetoableChangeSupport.java:65)
              at uk.co.capacitas.smart.network.ChangeAdapter.addListener(NetworkElement.java:428)
              at uk.co.capacitas.smart.network.NetworkElement.addListener(NetworkElement.java:251)
              at uk.co.capacitas.smart.network.CompositeNetworkElement.addComponent(CompositeNetworkElement.java:100)
              at uk.co.capacitas.smart.network.NetworkNode.addNodeInterface(NetworkNode.java:229)
              at uk.co.capacitas.smart.network.NetworkBuilder.addNetworkSiteService(NetworkBuilder.java:71)
              at uk.co.capacitas.smart.network.ConcreteNetworkManager.addNetworkSiteService(ConcreteNetworkManager.java:141)
              at uk.co.capacitas.smart.testharness.TestBuilder.addSites(TestBuilder.java:234)
              at uk.co.capacitas.smart.testharness.TestBuilder.buildNetwork(TestBuilder.java:101)
              at uk.co.capacitas.smart.testharness.TestBuilder.buildNetwork(TestBuilder.java:121)
              at uk.co.capacitas.smart.testharness.TestHarness.build(TestHarness.java:51)
              at uk.co.capacitas.smart.testharness.TestHarnessTest.testRun(TestHarnessTest.java:50)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at uk.co.capacitas.smart.testharness.TestHarnessTest.main(TestHarnessTest.java:26)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.beans.*;

      public class Tester {
          
          public Tester() {
          }
          
          public static void main(String[] args) {
              Tester tester = new Tester();
              VetoableChangeSupport changeSupport = new VetoableChangeSupport(tester);
              
              VetoableChangeListener listener = new VetoableChangeListener(){
                  public void vetoableChange(PropertyChangeEvent e) {
                  
                  }
              };
              VetoableChangeListenerProxy proxy = new VetoableChangeListenerProxy("property",listener);
              changeSupport.addVetoableChangeListener(proxy);
              
          }
          
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Not using a proxy works fine
      (Incident Review ID: 185508)
      ======================================================================

            malenkov Sergey Malenkov (Inactive)
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: