Results 1 to 4 of 4
  1. #1
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default Error = Null pointer with inner class

    Hi,
    I am trying out using innerclasses with GUI but I keep getting the following error msg. Can someone please help me figure it out. Thanks..

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Ex2201$Listen.actionPerformed(Ex2201.java:42)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown 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.plaf.basic.BasicButtonListener.mouseRe leased(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(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn 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.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)



    Java Code:
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.*;
    
    public class Ex2201 
    {
    	JTextField j1,j2;
    	JLabel label;
    	
    	public static void main(String[] args)
    	{
    		Ex2201 add = new Ex2201();
    	}
    	public Ex2201()
    	{
    		JFrame frame = new JFrame("Add numbers");
    		frame.setSize(300, 300);
    		frame.setLocation(500, 500);
    		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    		JPanel panel = new JPanel();
    		JTextField j1 = new JTextField(5);
    		JTextField j2 = new JTextField(5);
    		JButton button = new JButton("Calculate");
    		JLabel label = new JLabel();
    		
    		frame.add(panel);
    		panel.add(j1);
    		panel.add(j2);
    		panel.add(button);
    		panel.add(label);
    		frame.setVisible(true);
    		
    		Listen listen = new Listen();
    		button.addActionListener(listen);
    	}
    	public class Listen implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    
    			double d1 = Double.parseDouble(j1.getText());
    			double d2 = Double.parseDouble(j2.getText());
    			label.setText("The sum is " + (d1 + d2));
    			
    		}
    	}
    	
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jonytek View Post
    Hi,
    I am trying out using innerclasses with GUI but I keep getting the following error msg. Can someone please help me figure it out. Thanks..
    Java Code:
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.*;
    
    public class Ex2201 
    {
    	JTextField j1,j2; // [color="red"] *** LOCATION #1 ***[/color]
    	JLabel label;
    	
    	public static void main(String[] args)
    	{
    		Ex2201 add = new Ex2201();
    	}
    	public Ex2201()
    	{
    		JFrame frame = new JFrame("Add numbers");
    		frame.setSize(300, 300);
    		frame.setLocation(500, 500);
    		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    		JPanel panel = new JPanel();
    		JTextField j1 = new JTextField(5); // [color="red"] *** LOCATION #2 ***[/color]
    		JTextField j2 = new JTextField(5);
    		JButton button = new JButton("Calculate");
    		JLabel label = new JLabel();
            ///.....
    
    	public class Listen implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			// [color="red"] *** LOCATION #3 ***[/color]
    			double d1 = Double.parseDouble(j1.getText());
    			double d2 = Double.parseDouble(j2.getText());
    			label.setText("The sum is " + (d1 + d2));
    			
    		}
    	}
    	
    }

    Some observations:
    • LOCATION #1: You declare the j1 and j2 variables in the class. These variables are never initialized.
    • LOCATION #2: You re-declare the j1 and j2 variables in the constructor and instantiate them. These are separate unique objects completely unrelated to the j1 and j2 variables in the class.
    • LOCATION #3: You try to use the class j1 and j2 variables, the ones that have never been initiated!


    See the problem now and can you figure out its simple solution?
    Last edited by Fubarable; 05-11-2011 at 04:05 AM.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Classic example of local variables shadowing instance variables. Look very closely at your JTextFields.
    Last edited by Junky; 05-11-2011 at 04:05 AM. Reason: Damn j00 Fubar!

  4. #4
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default

    Thankyou guys. Your both legends!

    New code:

    Java Code:
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.*;
    
    public class Ex2201 
    {
    	JTextField j1 = new JTextField(5);
    	JTextField j2 = new JTextField(5);
    	JLabel label = new JLabel();
    	
    	public static void main(String[] args)
    	{
    		Ex2201 add = new Ex2201();
    	}
    	public Ex2201()
    	{
    		JFrame frame = new JFrame("Add numbers");
    		frame.setSize(300, 300);
    		frame.setLocation(500, 500);
    		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    		JPanel panel = new JPanel();
    		JButton button = new JButton("Calculate");
    		
    		
    		frame.add(panel);
    		panel.add(j1);
    		panel.add(j2);
    		panel.add(button);
    		panel.add(label);
    		frame.setVisible(true);
    		
    		Listen listen = new Listen();
    		button.addActionListener(listen);
    	}
    	public class Listen implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    
    			double d1 = Double.parseDouble(j1.getText());
    			double d2 = Double.parseDouble(j2.getText());
    			label.setText("The sum is " + (d1 + d2));
    			
    		}
    	}
    	
    }

Similar Threads

  1. Null Pointer Exception error
    By tfitz666 in forum New To Java
    Replies: 3
    Last Post: 03-28-2010, 07:20 PM
  2. Null Pointer
    By theen3my in forum AWT / Swing
    Replies: 3
    Last Post: 10-03-2009, 02:10 PM
  3. Null pointer exception
    By talha06 in forum JDBC
    Replies: 5
    Last Post: 07-14-2009, 01:12 AM
  4. null pointer help
    By mayhewj7 in forum New To Java
    Replies: 5
    Last Post: 02-17-2009, 11:51 PM
  5. Null pointer exception error
    By brownie_jedi in forum New To Java
    Replies: 3
    Last Post: 03-15-2008, 06:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •