简体   繁体   中英

Adding Components To JFrame in separate Thread

I am writing a wizard of sorts and would like to switch what is displayed using methods. Every time I run this code I get a null pointer exception.

    public class EventDispatch {

       public static void main(String [] args){
            WizardScreen wiz = new WizardScreen();
            new Thread(wiz).start();
            wiz.welcomeScreen();
       }
    }

    public class WizardScreen implements Runnable{

protected JFrame wizardFrame;
protected JPanel contentPane;
protected JButton newQuote; 
protected JButton openQuote;
protected JLabel title;
GridBagConstraints c;


public WizardScreen(){
    wizardFrame = new JFrame();
    contentPane = new JPanel(new GridBagLayout());
    wizardFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    wizardFrame.setSize(550, 450);
    wizardFrame.setResizable(false);
    wizardFrame.setLocationRelativeTo(null);
    wizardFrame.setTitle("Welcome!");
    wizardFrame.setContentPane(contentPane);
    wizardFrame.setVisible(true);
}

@Override
public void run() {
    System.out.println("Running wizardScreen");

}

public void welcomeScreen(){
    title = new JLabel("Welcome to ExSoft Quote Calculator Alpha 1.0");
    c.gridx = 0;
    c.gridy = 0;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = .5;
    contentPane.add(title, c);
    wizardFrame.validate();
    contentPane.repaint();
}

}

What am I doing wrong?

Take a walk through your code...

First, you create an instance of WizardScreen this initialises

  • wizardFrame
  • contentPane

Second, you start a Thread ...

Third, you call welcomeScreen on the instance of WizardScreen , this initialises...

  • title

It then tries to access the gridx property of c ...which hasn't yet been initialised...

You should have checked the information that the NullPointerException was giving you...

Exception in thread "main" java.lang.NullPointerException
    at eventdispatch.EventDispatch$WizardScreen.welcomeScreen(EventDispatch.java:52)
    at eventdispatch.EventDispatch.main(EventDispatch.java:20)

It clear states where the exception occurred, this is invaluable information both to you and us.

Beware, Swing is not thread safe, all interactions and modifications to the UI are expected to occur from within the context of the Event Dispatching Thread. See Concurrency in Swing

FYI:

It's generally advisable to use pack of setSize , which should be done last, right before you call setVisible . Also beware, that using setResizable(false) changes the size of the window...

wizardFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//wizardFrame.setSize(550, 450);
//wizardFrame.setResizable(false);
//wizardFrame.setLocationRelativeTo(null);
wizardFrame.setTitle("Welcome!");
wizardFrame.setContentPane(contentPane);
wizardFrame.setResizable(false);
wizardFrame.pack();
wizardFrame.setLocationRelativeTo(null);
wizardFrame.setVisible(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM