Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Trouble clearing JTextField, getting NullPointerException

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    My Mood
    Sleepy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Trouble clearing JTextField, getting NullPointerException

    I'm new to Java programming. I'm using JDK 1.7.
    I can not get this field to clear, tired many different methods i've seen online.
    Not sure what i'm doing wrong. Can someone please help?
    Here is my SSCCE, there is 3 different Classes.

    import java.awt.*;
    import javax.swing.*;
     
     
    public class TheLayout extends JFrame {
    	 private static final long serialVersionUID = 1L;
    	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++ ;
     
    	}
    }

    I'm not concerned with the output to work properly just want to get JTextField displayDown to clear for now.


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Trouble clearing JTextField, getting NullPointerException

    Quote Originally Posted by Lanst83 View Post
    ...JDK 1.7...I can not get this field to clear...
    public class TheEvent implements ActionListener, Runnable { 
     
        TheLayout gui;
        TheData data;
    .
    .
    .
        public void actionPerformed(ActionEvent event) {
    .
    .
    .
                data.setDownInt();  //<---data???
    .
                gui.displayDown.setText("  ");  //<---gui???       
    .
        }
    .
    .
    .
    .
    .
    	   }
    Your TheEvent class has instance variables gui and data that are never initialized.


    One possibility that uses the organization you have set up: Make a constructor for TheEvent that takes two parameters (with types TheLayout and TheData), from which it sets values for the two instance variables.

    Then in TheLayout, where you create your TheEvent object, give its constructor the arguments that it needs.


    Cheers!

    Z
    Last edited by Zaphod_b; September 9th, 2012 at 02:40 PM.

  3. The Following User Says Thank You to Zaphod_b For This Useful Post:

    Lanst83 (September 9th, 2012)

  4. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    My Mood
    Sleepy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trouble clearing JTextField, getting NullPointerException

    Thank you. Now the TextField clears but it is clearing in a new window. What have I done wrong?


     public void changeText() {
     
    	TheLayout gui = new  TheLayout();
    	gui.setVisible(false); // I have set this to false trying to keep it from making a new window,
    // but the old window still holds text
    	 gui.displayDown.setText("");		
     
    	}
     
     
    	   public void actionPerformed(ActionEvent event) {
    			 String command = event.getActionCommand();
    		      if ( command == "PASS"){ 
    		    	run();
     
    		    	TheData data = new TheData();
    		    	changeText();
    		    	data.setDownInt();
     
     
     
    		      }
     
    		}
    Last edited by Lanst83; September 9th, 2012 at 05:11 PM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trouble clearing JTextField, getting NullPointerException

      TheLayout gui = new  TheLayout();

    Why create a new TheLayout object? You need to get a reference to the current one that is visible so you can make changes to it. The posted code creates a new instance and makes changes it it, not to the currently viewed one.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Lanst83 (September 9th, 2012)

  7. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    My Mood
    Sleepy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trouble clearing JTextField, getting NullPointerException

    That makes perfect sense but I can't figure out how. Have tried creating different methods in which none work.
    I'm sure there are some basic fundamentals I'm missing so please be patient with me. Is there any good tutorials on this online?

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trouble clearing JTextField, getting NullPointerException

    Where is there a reference to the currently visible instance of TheLayout? How can a copy of that reference be passed to the class or method that needs to use it?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    My Mood
    Sleepy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trouble clearing JTextField, getting NullPointerException

    This is where I am at now. Trying to create a method that I can call that will clear the Field, but I feel like I've got some thing backwards. Thedata class is not used right now cause i thought that code be interferring. Im creating a new Thedata class for no reason right now. I realize that.


    public class TheLayout extends JFrame {
    	  public void changeText(Object JTextField) {
     
     
    		   this.displayDown.setText("");		
     
     
    		     }

     
    public class TheEvent implements ItemListener, ActionListener,
    Runnable { 
     
     
    TheLayout gui;
     
    	public void run() {
     
    		  OffenseFrame frame = new OffenseFrame();
     
     
     
     
     
     
    	}
     
     
    	   public void actionPerformed(ActionEvent event) {
    			 String command = event.getActionCommand();
    		      if ( command == "PASS"){ 
    		    	run();
     
     
    		    	TheData data = new TheData();
    		    	gui.changeText(gui.displayDown) ;
     
     
     
     
    		      }
     
    		}

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trouble clearing JTextField, getting NullPointerException

    One problem I see is the code is using == to compare Strings. It should use the equals() method.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #9
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Trouble clearing JTextField, getting NullPointerException

    Quote Originally Posted by Lanst83 View Post
    ...new window...wrong
    Quote Originally Posted by Lanst83 View Post
    ...I've got some thing backwards...
    I will try again. You can take my word for it or not, but I'm telling you that I wouldn't make suggestions like these unless I had tested them with your code.

    Maybe you can go back to your original code and try the two changes that I suggested.

    I hate to repeat myself, but:
    Quote Originally Posted by Zaphod_b
    Make a constructor for TheEvent that takes two parameters (with types TheLayout and TheData), from which it sets values for the two instance variables.
    So, in the TheEvent.java, make the first part of TheEvent class look something like
    public class TheEvent implements ActionListener, Runnable {
        TheLayout gui;
        TheData data;
     
        // Constructor sets values of gui and data to parameter values
        // passed by the calling function
        TheEvent(TheLayout g, TheData d) {
            /* Your code goes here.  It does two things */
            //Set gui  equal to the first parameter value.
            //Set data equal to the second parameter value
        }
    .
    . // The rest of your code
    .

    Next: I hate to repeat myself (again) but

    Quote Originally Posted by Zaphod_b
    Then in TheLayout, where you create your TheEvent object, give its constructor the arguments that it needs.
    So, in TheLayout.java, the class definition could begin with something like
    public class TheLayout extends JFrame {
        private static final long serialVersionUID = 1L;
     
        TheData data = new TheData();
        // Use arguments this and data to the constructor so that
        // the event object will be working on things that are
        // defined here.
        // First argument will be a reference to an object of
        // type TheLayout:  this
        // Second argument will be a reference to an object of
        // type TheData:    data
        //                 (or this.data if you like to type extra stuff)
        /* Your code for instantiating the event object goes here */
        TheEvent event = new TheEvent(First argument, Second argument);
    .
    . // The rest of your code
    .

    For now, leave TheData.java just as you originally posted it.


    I am hopeful that these two changes can get you far enough along to start debugging.

    I mean, you could re-organize the whole thing, and maybe you will decide to do things differently once you get into the real program development, but it could work OK the way you started. Having different classes for layout, event handling and data manipulation doesn't seem (to me) to be a bad idea at all.


    Cheers!

    Z
    Last edited by Zaphod_b; September 10th, 2012 at 10:57 AM.

  12. The Following User Says Thank You to Zaphod_b For This Useful Post:

    Lanst83 (September 21st, 2012)

  13. #10
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    My Mood
    Sleepy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trouble clearing JTextField, getting NullPointerException

    thanks . i'll give it a try

  14. #11
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    My Mood
    Sleepy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trouble clearing JTextField, getting NullPointerException

    It works!!! thanks everyone. How do i set this thread as solved?

  15. #12
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble clearing JTextField, getting NullPointerException

    Quote Originally Posted by Lanst83 View Post
    It works!!! thanks everyone. How do i set this thread as solved?
    There is a link near the top of the page named Thread Tools

Similar Threads

  1. NullPointerException trouble
    By jdaniel007 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2012, 03:05 PM
  2. Clearing the screen
    By ranjithfs1 in forum Java Theory & Questions
    Replies: 4
    Last Post: March 19th, 2012, 10:04 AM
  3. Help with clearing variables
    By fmr in forum Other Programming Languages
    Replies: 2
    Last Post: July 18th, 2011, 10:18 AM
  4. Clearing text from the console
    By sp11k3t3ht3rd in forum Java Theory & Questions
    Replies: 5
    Last Post: December 3rd, 2010, 05:36 AM
  5. JTextField clearing problem
    By rushhour in forum AWT / Java Swing
    Replies: 1
    Last Post: October 24th, 2010, 12:55 PM