Solved

Exception during event dispatching

Posted on 2007-11-17
6
435 Views
Last Modified: 2008-02-01
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)
0
Comment
Question by:zolf
  • 4
6 Comments
 
LVL 13

Accepted Solution

by:
Bart Cremers earned 250 total points
ID: 20304001
Can you post line 505 here? And a few before?

The exception says that something on the line is not instantiated (still is null), while you're trying to call a method on it.
0
 
LVL 23

Assisted Solution

by:rama_krishna580
rama_krishna580 earned 250 total points
ID: 20304751
At line 505 of AddNewCustomer.java; the variable/method call is expecting some value but its null....check this line what you have .....????

R.K
0
 

Author Comment

by:zolf
ID: 20306512
     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;
          }
      }
}
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:zolf
ID: 20306522

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);
0
 

Author Comment

by:zolf
ID: 20306557

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
0
 

Author Comment

by:zolf
ID: 20306561

got it.

thanks mate
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
factorial example 4 39
javap not working 8 44
login form jsp example 2 24
How to determine if a string is a valid SHA value 7 29
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question