0

Im working on creating a basic GUI for a program I was tasked with early on in the year. My goal is to have it so that when the program is executed, the first screen that displays is a field for the user to type out their name. Upon pressing enter or the submit button this then directs them to another window which will have the name of the player embedded as a layer.

Now I'm not sure I am doing this in the most efficient way but from reading through multiple answers, creating separate frames seems like the best way to do this, although do correct me if I am wrong.

I get the following error when I execute

"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"

So I begin by running the loginpanel code:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class loginPanel extends JFrame {

    public static void main(String[] args) {
        loginPanel loginFrame = new loginPanel();
    }

    private JButton submit;
    private JLabel playerName;
    private JTextField nameInput;
    public String name;

    JPanel panel = new JPanel();

    loginPanel() {
        setTitle("Game Data");
        setSize(300, 400);
        setLocationRelativeTo(null);

        submit = new JButton("Submit");
        submit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {

                if (nameInput.getText().isEmpty()) {
                    JOptionPane.showMessageDialog(null, "Invalid Input, please try again");
                }

                else {
                    name = nameInput.getText();
                    gameDataPanel mainFrame = new gameDataPanel();
                    mainFrame.setVisible(true);
                    dispose();
                }
            }

        });

        playerName = new JLabel("Enter Player Name");

        nameInput = new JTextField(30);
        nameInput.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {

                if (nameInput.getText().isEmpty()) {
                    JOptionPane.showMessageDialog(null, "Invalid Input, please try again");
                }

                else {
                    name = nameInput.getText();

                    gameDataPanel mainFrame = new gameDataPanel();
                    mainFrame.setVisible(true);
                    dispose();
                }
            }

        });

        panel.add(playerName);
        panel.add(nameInput);
        panel.add(submit);

        getContentPane().add(panel);
        pack();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public String grabName() {
        return nameInput.getText();
    }

}

and then it should run this upon pressing submit:

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class gameDataPanel extends JFrame {

    private JLabel playerName, gameName, gameScore, timePlayed;
    private JTextField gameNameField, gameScoreField, timePlayedField;
    private loginPanel login;
    private String name2;

    public static void main(String[] args) {
        gameDataPanel mainFrame = new gameDataPanel();
    }

    JPanel panel = new JPanel();

    gameDataPanel() {
        setTitle("Game Data Entry Program");
        setBackground(Color.WHITE);
        setPreferredSize(new Dimension(400, 300));


        name2 = login.name;

        playerName.setText(name2);

        panel.add(playerName);
        getContentPane().add(panel);
        pack();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

The problem is that it never gets past the first screen, any help is appreciated. Thanks!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
SkyPlus
  • 105
  • 1
  • 9

0 Answers0