Skip to main content

Can't get JTextField to clear and getting a NullPointerException

3 replies [Last post]
Lants83
Offline
Joined: 2012-09-09
Points: 0

I am not concerned with getting the proper output, just don't know what i'm doing wrong. Just want to atleast get the JTextField to clear.
Here is my code. There is 3 different classes here. Can someone please help.

import java.awt.*;
import javax.swing.*;
</pre>
<pre>

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");
</pre>     
<pre>

  public TheLayout() {
         super("THE FOOTBALL GAME");
</pre>      
<pre>         setSize(750, 270);

</pre>        
<pre>         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GridLayout layout = new GridLayout(5, 1, 10, 10);
         setLayout(layout);
</pre>         
<pre>

</pre> 
<pre> displayDown.setText(data.getDown());

        displayDown.setEditable(true);       //Would like this to be false
            add(displayDown);
</pre>
<pre>   FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER,

                10, 10);
   pass.addActionListener(event);
   row1.setLayout(layout1);
   option.add(pass);
   row1.add(pass);
   add(row1);
   setVisible(true);
     }
</pre>
<pre>  public static void main(String[] arguments) {

    TheLayout frame = new TheLayout();

}
}
</pre>
<pre>

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
</pre>
<pre>

</pre>
<pre>public class TheEvent implements ActionListener,

Runnable {
</pre>
<pre>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("  ");


      }

   }
</pre>
<pre>

</pre>
<pre>

</pre>
<pre>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++ ;

}
}
  
      

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tutortutor
Offline
Joined: 2012-09-09
Points: 0

In class TheEvent's void actionPerformed(ActionEvent event) method, you are executing data.setDownInt();. However, data contains the null reference because this field variable is not being initialized. This is the reason for the thrown java.lang.NullPointerException instance.

I hope this helps.

Jeff
tutortutor.ca

Lants83
Offline
Joined: 2012-09-09
Points: 0

I have added in TheEvent's void actionPerformed(ActionEvent event) method

TheData data = new Thedata();
TheLayout gui = new TheLayout();

But I have created a new window with the JTextField cleared which is not correct. soo...

in the Layout Class I added

public void changeText(Object JTextField) {
  this.displayDown.setText("");
   }

then added in the TheEvent Class
gui.changeText(gui.displayDown) ;
and removed the TheLayout gui = new TheLayout()

no longer have the NullPointerException but the JTextField doesnt clear.
can't find the proper way any where. this is getting discouraging.

tutortutor
Offline
Joined: 2012-09-09
Points: 0

I notice that you previously specified, in the actionPerformed(ActionEvent) method, gui.displayDown.setText("  "); to clear the textfield. Although this method call results in the textfield looking empty, it isn't. Instead, the textfield contains two spaces. You can clear the textfield by passing the empty string in the method call, as in gui.displayDown.setText("");.

I hope this helps.

Jeff
tutortutor.ca