Kyle
Kyle

Reputation: 3042

Java application: Getting input before running

public class checkUsernames extends JFrame {
private static JTextArea textArea1;
private static JButton button1;
private static JScrollPane scrollPane1;
private static JTextField textField1;
private static JPasswordField passwordField1;
private static JLabel label3;
private static JButton button2;
private static JLabel label1;
private static JLabel label2;

public checkUsernames() {
    initComponents();
}

public static void main(String[] args) throws IOException {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Nimbus isn't available");
        }

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                checkUsernames GUI = new checkUsernames();
                GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                GUI.setVisible(true);
            }
        });
        String username = textField1.toString();
        String password = passwordField1.toString();

Exception in thread "main" java.lang.NullPointerException

checkUsernames is the only class. When I try to run the application the program executes further (proceeding without String username & String password) without waiting for input. How can I fix this?

Upvotes: 0

Views: 707

Answers (4)

MockerTim
MockerTim

Reputation: 2483

Why dont you use SwingUtilities.invokeAndWait() instead? This seems to solve your problem immediately.

Here is the simple example:

import javax.swing.SwingUtilities;

public class InvokeAndWaitExample {
    public static void main (String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable () {
                public void run () {
                    System.out.println("Hello World on " + Thread.currentThread());
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Finished on " + Thread.currentThread());
    }
}

Upvotes: 1

duffymo
duffymo

Reputation: 308763

None of your private static members appear to be initialized at all. You won't get past the NullPointerException until you initialize each and every object you create before you use it.

I have no idea what you're talking about. You have to do something like this for all those objects:

private static JTextArea textArea1 = new JTextArea();

You aren't entering a username or password; you're creating a text area UI element that can accept them when you do enter them.

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168825

If you want the code execution to stop while waiting for input, make the JFrame a modal JDialog or JOptionPane instead.

Upvotes: 2

d-live
d-live

Reputation: 8036

Well invokeLater will invoke the code later. It dosent gurantee textfield1.toString() to execute after checkUserNames()

Upvotes: 1

Related Questions