Question

I am trying to make a simple GUI word dragging game and the way I structured the code is that I have a Driver class that sets up the main JFrame and a JPanel where the words are to be contained in and a JButton that prompts a Popup class to ask for a new word to add. and then creates a WordBox. My problem seems to arise from the fact that the Popup class is a sub-class (I think thats the correct term) and so it seems that there seems to be an extra Popup class as a layer between the Driver and the actual Popup with the WordBox. I know its confusing, but here is some of the code:

public class Popup extends JFrame implements ActionListener {

JLabel lblPrompt;
JTextField txtWord;
JButton btnOK;
BoxWord w;
static BoxWord word;


public Popup(){
    //window formatting was here

    btnOK.addActionListener(this);
} 

@Override
public void actionPerformed(ActionEvent e){
    w = new BoxWord(txtWord.getText());
    //word = new BoxWord("it works");
}

public static void main(String[] args) {
    Popup p = new Popup();
    //System.out.println(word.getWord());
}

and

public class BoxWord extends JButton {

private String s = "";

public BoxWord(String word){
    this.s = word;
    this.setText(word);
}

public String getWord(){
    return s;
}
}

and the Driver:

public class Driver extends JFrame implements ActionListener{

JButton addWord;
JPanel panel;
int x, y;

public Driver(){
    //Window formatting was here


    addWord.addActionListener(this);

} 

public static void main(String[] args) {
    Driver d = new Driver();
}
@Override 
public void actionPerformed(ActionEvent e){//make a new popup to ask for word
    Popup p = new Popup();
    BoxWord w = p.w;
    System.out.println(w.getWord());
    w.setLocation(100,100);
}

My error is (at least the begining of it. the whole thing is like 20 lines long):

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at worddrag.Driver.actionPerformed(Driver.java:42)

The line 42 is the

System.out.println(w.getWord());

I feel this is a very trivial and simple problem but I cannot figure out why I keep getting the error. I essentially need to be able to access the BoxWord in the Popup actionPerformed method from the Popup main method. any and all help is appreciated. My apologies for the wall of text

Was it helpful?

Solution

w is obviously null when you try to access it.

Your problem is that you're using a JFrame where a modal dialog or JOptionPane would work better. If you used the dialog, then your calling code would wait until the dialog is no longer visible, and at that time, w would likely no longer be null.


Edit
You ask:

and can you elaborate on why JFrame will not work? Just telling me that its my problem doesnt help very much -

What? I didn't just tell you that it was the problem -- I offered a solution -- use a modal JDialog. Again, a modal dialog will freeze code flow from the calling code when it is launched, and the calling code will remain frozen until the dialog is no longer visible.

Your problem is that your attempting to use the w variable before the dialog window has had a chance to do anything with it. A JFrame does not pause the calling code's program flow, and that is causing your problem.


Edit 2

So a JFrame doesn't actually execute the code until it is closed?

No, not true at all. Look where your w is given a valid reference -- in your JFrame's button's ActionListener. So w will not receive a valid reference until the ActionListener has been called, which only will happen when the button is pressed. Your code as written tries to use w immediately before the user has had any chance to push your pop-up's buttons. If you used a modal JDialog instead, and made sure that w was set prior to closing the dialog, your code could work.

But a JDialog will do so as it gets the input?

Nope. See above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top