Results 1 to 3 of 3
  1. #1
    Jaeela is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException help

    Well I've been writing a swing program to the the greatest common divisor and reduce a fraction, but I have been getting a null pointer exception inside the actionlistener named "findlisten", on the line that says "String str1 = GCDFinder.textfield1.toString();". Here is my error:
    Java Code:
     Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at gcd.main.GCDFinder$1.actionPerformed(GCDFinder.java:90)
    	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.plaf.basic.BasicButtonListener.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)
    and here is my code:
    Java Code:
     import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class GCDFinder {
    public static int a;
    public static int b;
    public static JTextField textfield1;
    public static JTextField textfield2;
    public static JFrame frame2;
    
    	public static void main(String[] args) {
    		showGUI();
    		
    	}
    	public static void showGUI() {
    		JLabel label1 = new JLabel("Numerator:");
    		JLabel label2 = new JLabel("Denominator:");
    		JLabel label7 = new JLabel();
    		JTextField textfield1 = new JTextField("0");
    		JTextField textfield2 = new JTextField("0");
    		JButton findbutton = new JButton("Find");
    		findbutton.addActionListener(findlisten);
    		JFrame frame1 = new JFrame();
    		frame1.setTitle("GCDFinder");
    		frame1.setSize(200,130);
    		frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Container pane=frame1.getContentPane();
    		pane.setLayout(new GridLayout(3,1));
    		pane.add(label1);
    		pane.add(textfield1);
    		pane.add(label2);
    		pane.add(textfield2);
    		pane.add(label7);
    		pane.add(findbutton);
    		frame1.setVisible(true);
    		
    	}
    	public static void endGUI(int a, int b) {
    		int rGCD = gcd(a, b);
    		int num = a / rGCD;
    		int den = b / rGCD;
    		JFrame frame2 = new JFrame();
    		frame2.setSize(200,200);
    		frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JButton closebutton = new JButton("close");
    		JButton againbutton = new JButton("again");
    		closebutton.addActionListener(closelisten);
    		againbutton.addActionListener(againlisten);		
    		JLabel label3 = new JLabel("Your GCD is:" + rGCD);
    		JLabel label4= new JLabel("The numerator is:" + num);
    		JLabel label5 = new JLabel("The denominator is:" + den);
    		Container pane2 = frame2.getContentPane();
    		pane2.setLayout(new GridLayout(4,1));
    		pane2.add(label3);
    		pane2.add(label4);
    		pane2.add(label5);
    		pane2.add(againbutton);
    		pane2.add(closebutton);
    		frame2.setVisible(true);
    	}
    	public static int gcd(int p, int q) {
            while (q != 0) {
                int temp = q;
                q = p % q;
                p = temp;
            }
            return p;
    	
    	}
    
    	static ActionListener findlisten = new ActionListener() {
            public void actionPerformed(ActionEvent aj) {
    			 String str1 = GCDFinder.textfield1.toString();
    			 int a = Integer.parseInt(str1);
    				String str2 = GCDFinder.textfield2.toString();
    				int b = Integer.parseInt(str2);
    			 endGUI(a, b);
            }
          };
      	static ActionListener againlisten = new ActionListener() {
            public void actionPerformed(ActionEvent aj) {
          	  GCDFinder.frame2.setVisible(false);
        	  showGUI();
            }
          };
    
    		static ActionListener closelisten = new ActionListener() {
    	        public void actionPerformed(ActionEvent aj) {
    				  System.exit(0);
    	        }
    	      };
    
    }

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

    Default Re: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException help

    The JTextField displayed is not the one you're testing (which is null). Note that you re-declare textfield1 inside of your showGUI method, and so that is the one that is displayed. The variable declared in the class is never initiated. This is called variable shadowing.

    But much more important is you are grossly over-using and mis-using static here. None of your methods except the main should be static and none of the variables should be static.

  3. #3
    Jaeela is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException help

    Thank you Fubarable, it is working now. I will try to reduce my use of static.

Similar Threads

  1. Replies: 1
    Last Post: 12-13-2010, 04:19 PM
  2. Replies: 3
    Last Post: 04-30-2010, 08:44 PM
  3. Replies: 5
    Last Post: 08-26-2009, 12:37 PM
  4. Replies: 12
    Last Post: 10-05-2008, 07:52 AM
  5. Replies: 3
    Last Post: 01-29-2008, 02:37 AM

Tags for this Thread

Posting Permissions

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