Results 1 to 7 of 7
  1. #1
    LancePaxton is offline Member
    Join Date
    Sep 2012
    Location
    South Carolina
    Posts
    3
    Rep Power
    0

    Default JTextField setText problem

    I am having this problem. This method is not working for me. i am new to java and sure i am making a stupid mistake.
    When i try this i get a NullPointerException and the JtextField is not clearing. all code not showed using jdk 1.7

    public void TheLayout extends Jframe

    TheData data;
    JTextField displayMessage = new JTextFeild();
    JButton clear = new Jbutton("clear");
    add.ActionEventListener(data);
    displayMessage.setText("data.getMessage()");
    add(displayMessage);
    add("clear");
    ---New Class with action and method class---
    again not all code here.. think its enough
    public void TheData ..........
    public String getMessage() {

    i have code here to set message
    action listener class ().......
    if (command == "clear") {
    TheLayout gui;

    gui.displayMessage.setText(''');}
    also tried setText(null);
    Last edited by LancePaxton; 09-05-2012 at 02:42 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: JTextField setText problem

    Removed form Clean the content of the JTextField

    When you have a question, start your own thread. Son't post to 5 year old threads and don't hijack another poster's thread.

    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem. Not all your code, but not uncompilable snippets devoid of any context as already posted. But before posting anything more, go through these links:
    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum

    Do you really have a variable named add that has a method ActionEventListener? If oyu do, please go through Code Conventions for the Java Programming Language: Contents -- method names should start with a lowercase letter.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    LancePaxton is offline Member
    Join Date
    Sep 2012
    Location
    South Carolina
    Posts
    3
    Rep Power
    0

    Default Problem with clearing JTextField. Getting NullPointerException

    I have seen several problems related to this but can't seem to solve this problem.
    I am not concerned with getting the correct output in the JTextField at
    this time but I would like to at least make the field blank and not get a NullPointerException.
    Then I can work on getting the correct result. Please check my SSCCE at google docs.
    Please don't be to hard on me for mistakes, I am learning. Thank you for your time.

    https://docs.google.com/document/pub...VQBsfpFIXRke4E

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: JTextField setText problem

    Don't double post. I've merged the new thread you started.

    Please check my SSCCE at google docs.
    If it's short, it should be possible to post it here. If it's not short, then it's too much to expect forum members to take time over.

    Moreover, many file sharing sites are blocked by corporate firewalls, so the odd member who is willing to click an external link (I won't) may not be able to get there. Additionally, what's to stop you from taking down the code and leaving this thread with a dead link, of no use to anyone?

    A forum is for sharing problems and their possible solutions -- not a free helpdesk.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    LancePaxton is offline Member
    Join Date
    Sep 2012
    Location
    South Carolina
    Posts
    3
    Rep Power
    0

    Default Help before Darryl locks this please

    I can't get the JTextField displayDown to clear and not get a NullPointerException. I was told this isnt a help desk but i see alot of people getting help
    here is all the code i believe is relevant. Just want it to clear then I'll figrue out how to get the program to run corrctly as i go.
    Darrly says my code has to be able to compile. so here it is please note this is 3 classes. Darryl I know you don't want to help
    you made that clear.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    
    public class TheLayout extends JFrame {
    	
    	TheEvent event = new TheEvent();
    	TheData data = new TheData();
    	
         JTextField displayDown = new JTextField("");
         ButtonGroup option = new ButtonGroup();
         JPanel row1 = new JPanel();
         JButton pass = new JButton("PASS");
         
    
      public TheLayout() {
             super("THE FOOTBALL GAME");
          
             setSize(750, 270);
            
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            GridLayout layout = new GridLayout(5, 1, 10, 10);
             setLayout(layout);
             
    
     
     displayDown.setText(data.getDown());
            displayDown.setEditable(true);       //Would like this to be false
                add(displayDown);
    
       FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER,
                    10, 10);
       pass.addActionListener(event);
       row1.setLayout(layout1);
       option.add(pass);
       row1.add(pass);
       add(row1);
       setVisible(true);
         }
    
      public static void main(String[] arguments) {
    	    TheLayout frame = new TheLayout();
    	 
    	}
     }
    
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    
    
    
    public class TheEvent implements ActionListener,
    Runnable { 
    
    TheLayout gui;
    TheData data;
    	
    	
    	
    	public void run() {
    		// TODO Auto-generated method stub
    		
    	}
    	
    	
    	   public void actionPerformed(ActionEvent event) {
    		String command = event.getActionCommand();
    		      if ( command == "PASS"){ 
    		    	run();
    		    	data.setDownInt();
    		    // int num = Integer.parseInt("0" + data.displayDown());  // working on this
    		    	gui.displayDown.setText("  ");			
    				 
    				 
    		      }
    			
    	   }
    
    
    
    
    
    public class TheData {
    	TheLayout gui;
    	TheEvent event;
    	public int down ; 
    	public String theDown;
    	public int yardsGain;
    	
    	
    	
    	public String getDown() {
    		this.down = getDownInt();
    			if (down == 1){theDown = (" FIRST DOWN");}
    			if (down == 2){ theDown = (" SECOND DOWN"); }
    			if (down == 3){ theDown = (" THIRD DOWN"); }
    			if (down == 4){ theDown = (" FOURTH DOWN"); }
    			if (down  < 1 ||down > 4) { theDown = ("ERROR WITH DOWNS");}
    			return theDown;
    			}
    	
    	public int getDownInt() {
    		return this.down;
    	}
    public void setDownInt() {
    		
    		this.down = this.down++ ;
    	
    	}
    }

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: JTextField setText problem

    You have been alerted to the Forum Rules, which specifically state
    Please do not start more than one topic (in the same OR different forums) with the same question or message.
    You have been specifically told about this:
    Quote Originally Posted by DarrylBurke View Post
    Don't double post.
    Any more threads with the same question and you will face a ban.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Help before Darryl locks this please

    Quote Originally Posted by LancePaxton View Post
    Darryl I know you don't want to help
    you made that clear.
    Don't put words in my mouth. State your assumptions for what they are: your assumptions.

    Oh, and forum members are going to be falling over themselves to help you after that. Not.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 08-30-2011, 09:18 AM
  2. problem with JLabel.setText();
    By nonabhai in forum AWT / Swing
    Replies: 5
    Last Post: 10-09-2010, 04:44 AM
  3. If statement and JLabel.setText() problem
    By peterhabe in forum New To Java
    Replies: 5
    Last Post: 07-25-2010, 10:11 AM
  4. setText() problem
    By Jozo in forum Java Applets
    Replies: 4
    Last Post: 04-27-2010, 05:29 AM
  5. setText() problem
    By jls7168 in forum New To Java
    Replies: 2
    Last Post: 02-20-2009, 10:34 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
  •