Link to home
Start Free TrialLog in
Avatar of Zolf
ZolfFlag for United Arab Emirates

asked on

Exception during event dispatching

when i run the code i get this error

Exception occurred during event dispatching:
java.lang.NullPointerException
      at src.com.AddNewCustomer$JMenuEmailActionListener.actionPerformed(AddNewCustomer.java:505)
      at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
      at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
      at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
      at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
      at javax.swing.AbstractButton.doClick(Unknown Source)
      at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
      at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
      at java.awt.Component.processMouseEvent(Unknown Source)
      at javax.swing.JComponent.processMouseEvent(Unknown Source)
      at java.awt.Component.processEvent(Unknown Source)
      at java.awt.Container.processEvent(Unknown Source)
      at java.awt.Component.dispatchEventImpl(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Window.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.Dialog$1.run(Unknown Source)
      at java.awt.Dialog$3.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.awt.Dialog.show(Unknown Source)
      at src.com.MainForm$1.actionPerformed(MainForm.java:819)
      at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
      at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
      at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
      at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
      at javax.swing.AbstractButton.doClick(Unknown Source)
      at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
      at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
      at java.awt.Component.processMouseEvent(Unknown Source)
      at javax.swing.JComponent.processMouseEvent(Unknown Source)
      at java.awt.Component.processEvent(Unknown Source)
      at java.awt.Container.processEvent(Unknown Source)
      at java.awt.Component.dispatchEventImpl(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Window.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)
ASKER CERTIFIED SOLUTION
Avatar of Bart Cremers
Bart Cremers
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zolf

ASKER

     ActionListener JMenuEmailActionListener = new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
                  String str = e.getActionCommand();
                  
                  Map<String, String> mapping = new HashMap<String, String>();
                JTextField linkedTextField = textField_Email_1;
                //JMenuItem currentItem;
                String currentItem;

                  if(str.equals("Email 1"))
                  {                  
                        emailLabel_1.setText(str);
                        mapping.put(str, linkedTextField.getText());
                        System.out.println(str+"  ,    "+ linkedTextField.getText());
                    String source =  e.getSource().toString();
                   
                    if (mapping.containsKey(source))
                    {
                        linkedTextField.setText(mapping.get(source));
                        System.out.println("IF");
                    }
                    else
                    {
                        linkedTextField.setText("");
                        System.out.println("ELSE");
                    }

                    currentItem = source;

                  }
                  else if(str.equals("Email 2"))
                  {
                        emailLabel_1.setText(str);
                        mapping.put(str, linkedTextField.getText());
                        System.out.println(str+"  ,    "+ linkedTextField.getText());
                        String source =  e.getSource().toString();

                    if (mapping.containsKey(source))
                    {
                        linkedTextField.setText(mapping.get(source));
                        System.out.println("IF");
                    }
                    else
                    {
                        linkedTextField.setText("");
                        System.out.println("ELSE");
                    }

                    source =  e.getSource().toString();
                  }
                  else if(str.equals("Email 3"))
                  {
                        emailLabel_1.setText(str);
                        mapping.put(str, linkedTextField.getText());
                        System.out.println(str+"  ,    "+ linkedTextField.getText());
                        String source =  e.getSource().toString();

                    if (mapping.containsKey(source))
                    {
                        linkedTextField.setText(mapping.get(source));
                        System.out.println("IF");
                    }
                    else
                    {
                        linkedTextField.setText("");
                        System.out.println("ELSE");
                    }

                    source =  e.getSource().toString();
                  }
            }
      };
      
      class JMenuEmailActionListener implements ActionListener {
          private final Map<JMenuItem, String> mapping = new HashMap<JMenuItem, String>();
          private final JTextField linkedTextField;
          private final JLabel labelForTextField;
          private JMenuItem currentItem;

          public JMenuEmailActionListener(JTextField linkedTextField, JLabel labelForTextField, JMenuItem currentItem) {
              this.linkedTextField = linkedTextField;
              this.labelForTextField = labelForTextField;
              this.currentItem = currentItem;
          }

          public void actionPerformed(ActionEvent e) {        ////////////////  505   / //////////////
              mapping.put(currentItem, linkedTextField.getText());

              JMenuItem source = (JMenuItem) e.getSource();
              labelForTextField.setText(source.getActionCommand());

              if (mapping.containsKey(source)) {
                  linkedTextField.setText(mapping.get(source));
              } else {
                  linkedTextField.setText("");
              }

              currentItem = source;
          }
      }
}
Avatar of Zolf

ASKER


emailLabel_1 = new JLabel();
            emailLabel_1.setText("Email 1");
            emailLabel_1.setBounds(512, 60, 43, 16);
            generalPane.add(emailLabel_1);
       
            JPopupMenuButton emailOpt = new JPopupMenuButton();
            JPopupMenu popupMenuEmail = new JPopupMenu();
            JMenuItem email_1 = new JMenuItem("Email 1");
            JMenuItem email_2 = new JMenuItem("Email 2");
            JMenuItem  email_3 = new JMenuItem("Email 3");
            
            ////email_1.setActionCommand("Email 1");
            ///email_2.setActionCommand("Email 2");
            ////email_3.setActionCommand("Email 3");
            
            ////email_1.addActionListener(JMenuEmailActionListener);
            ////email_2.addActionListener(JMenuEmailActionListener);
            ////email_3.addActionListener(JMenuEmailActionListener);
            
            //You can create it like this:

              ActionListener listener = new JMenuEmailActionListener(textField_Email_1, emailLabel_1, email_1);

      //and simply add it to the three menu items:

                  email_1.addActionListener(listener);
                  email_2.addActionListener(listener);
                  email_3.addActionListener(listener);
Avatar of Zolf

ASKER


when i enter a value in the textfield for the first menu item and then select the second menu item i get this error and i did i system.out.println
public void actionPerformed(ActionEvent e)
          {
                 System.out.println(linkedTextField+ "        " +currentItem.getText());
              mapping.put(currentItem, linkedTextField.getText());

i get this

null        Email 1
Avatar of Zolf

ASKER


got it.

thanks mate