Results 1 to 8 of 8
Like Tree2Likes
  • 2 Post By doWhile

Thread: setText() problems.

  1. #1
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default setText() problems.

    Java Code:
    package gui;
    
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class javaFP extends JApplet implements ActionListener{
    	JButton b;
    	JButton b1;
    	JButton b2;
    	JTextField field;
    	public String text;
    	public void init(){
    		try{
    		SwingUtilities.invokeAndWait(new Runnable(){
    			public void run(){
    				swingGUI();
    			}
    		});
    	}
    		catch(Exception e){
    			System.out.println(e);
    		}
    	}
    	public void swingGUI(){
    		
    		/*b= new JButton("Button 1"); 
    		b.addActionListener(
    				new ActionListener() {
    					public void actionPerformed(ActionEvent e) {
    						field.setText("Button 1");
    					}
    				}
    				);
    		b1= new JButton("Button 2"); 
    		b1.addActionListener(
    				new ActionListener() {
    					public void actionPerformed(ActionEvent e) {
    						field.setText("Button 2");
    					}
    				}
    				);*/
    		JButton b = new JButton(new AbstractAction ("Button 1") {
    			 public void actionPerformed( ActionEvent e ) {
    				 field.setText("Button 1");
    			 }
    		});
    
    		JButton b1 = new JButton(new AbstractAction ("Button 2") {
    			 public void actionPerformed( ActionEvent e ) {
    				 field.setText("Button 2");
    			 }
    		});
    		
    		JButton b2 = new JButton(new AbstractAction ("Button 3") {
    			 public void actionPerformed( ActionEvent e ) {
    				 field.setText("Button 3");
    			 }
    		});
    		
    		JTextField field = new JTextField(20);
    		setLayout(new FlowLayout());
    		setSize(300, 250);
    		setVisible(true);
    		add(b);
    		add(b1);
    		add(b2);
    		add(field);
    		field.setEditable(true);
    		
    		
    	}
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    }

    Using this code I keep getting an error. The way this is supposed to work is that when I click on any button, 1 2 or 3, it puts the name of the button into the TextField. When I click on a button all I get is this:

    Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at gui.javaFP$2.actionPerformed(javaFP.java:46)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6505)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:627 0)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:703)
    at java.awt.EventQueue.access$000(EventQueue.java:102 )
    at java.awt.EventQueue$3.run(EventQueue.java:662)
    at java.awt.EventQueue$3.run(EventQueue.java:660)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:676)
    at java.awt.EventQueue$4.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 673)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:97)


    No Yodas please...

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: setText() problems.

    Something is null in one of your action listeners...check that the variables you use are being instantiated (hint: if you look closely, you will see the one variable your reference will be null - a problem with variable scope)

  3. #3
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: setText() problems.

    Quote Originally Posted by doWhile View Post
    Something is null in one of your action listeners...check that the variables you use are being instantiated (hint: if you look closely, you will see the one variable your reference will be null - a problem with variable scope)
    If you know it why don't you tell me because I've looked over my code thousands of times. I came to ask a question not get asked them...

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default Re: setText() problems.

    What is a 'yoda'?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: setText() problems.

    Quote Originally Posted by JosAH View Post
    What is a 'yoda'?

    kind regards,

    Jos
    What you guys are doing.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: setText() problems.

    Quote Originally Posted by Army View Post
    If you know it why don't you tell me because I've looked over my code thousands of times. I came to ask a question not get asked them...
    Because learning to program is about problem solving, which is an art that takes years of practice. Being asked questions about your code, or taking suggestions, or trying things a bit differently - removing code here or adding code there to debug - helps give you that practice. Being told precisely the problem or exactly what to do does not. And for what its worth - I never asked you a question. But now that you mention it I will: where is the variable 'field' that you access in your ActionListener's instantiated? The answer to that question should be - it isn't. I do hope that another Yoda reference is not forthcoming...
    Fubarable and DarrylBurke like this.

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

    Default Re: setText() problems.

    Quote Originally Posted by JosAH View Post
    What is a 'yoda'?
    He likes not the socratic method of learning. Rather than putting forth effort, prefer to be spoon-fed the answer, would he.

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: setText() problems.

    Please be forthright when cross posting. We spend our time helping you, to find out someone has already answered your question elsewhere - shows how much you value our time
    x-posted at Swing Help
    Last edited by doWhile; 06-21-2012 at 07:49 AM. Reason: I to we

Similar Threads

  1. SwingWorker vs setText
    By mytremea in forum AWT / Swing
    Replies: 5
    Last Post: 11-13-2011, 04:26 PM
  2. setText() problem
    By Jozo in forum Java Applets
    Replies: 4
    Last Post: 04-27-2010, 06:29 AM
  3. setText in JTextArea
    By hero in forum AWT / Swing
    Replies: 1
    Last Post: 10-12-2009, 10:38 PM
  4. setText() problem
    By jls7168 in forum New To Java
    Replies: 2
    Last Post: 02-20-2009, 11:34 PM
  5. setText in event
    By sniezna.stopa in forum SWT / JFace
    Replies: 0
    Last Post: 06-20-2008, 03:56 PM

Posting Permissions

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