user2419358
user2419358

Reputation: 3

null pointer exception when calling textField.getText()

ok so I have a GUI with some textFields and combo boxes. Whenever I try I get the text textField.getText() and comboBox.getSelectedItem() I get a null pointer exception. even though my comboBox defaults to a set String or I have text in the textField. I run the GUI_preGame class and type stuff into the boxes and then call getText().

Sorry that the code is so messy (partly due to using window builder group layout) I call this Class from another by using GUI_preGame.PetNames(); Error is with this line: 498 (a print statement I put in to just print it!) System.out.println(textField_3.getText().toString());

package tamagotchiAssignment;

import java.awt.BorderLayout;

public class GUI_preGame extends JFrame {

    private static JPanel contentPane;
    private static JTextField textField;
    private static JTextField textField_1;
    private static JTextField textField_2;
    private static JTextField textField_3;
    private static JTextField textField_4;
    private static JTextField textField_5;
    private static JTextField textField_6;
    private static JTextField textField_7;
    private static JTextField textField_8;
    private static JTextField textField_9;
    private static JTextField textField_10;
    private static JTextField textField_11;
    private static JComboBox comboBox_2;
    private static JComboBox comboBox_3;
    private JLabel lblPlayer_1;
    private JLabel lblPlayer_2;
    private JLabel lblPet_1;
    private JLabel lblPet_2;
    private JLabel label;
    private static JComboBox comboBox_7;
    private static JComboBox comboBox_8;
    private static JComboBox comboBox_9;
    private JLabel label_1;
    private static JComboBox comboBox_10;
    private static JComboBox comboBox_11;
    private static JComboBox comboBox_12;
    private JLabel lblPet_4;
    private JLabel lblPet_5;
    private static ArrayList<String> playerTexts = new ArrayList<String>();
    private static ArrayList<String> petTexts = new ArrayList<String>();
    private static ArrayList<String> speciesTexts = new ArrayList<String>();
    private static JComboBox comboBox_4;
    private static JComboBox comboBox_5;
    private static JComboBox comboBox_6;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GUI_preGame frame = new GUI_preGame();
                    frame.setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public GUI_preGame() {
        setResizable(false);
        setTitle("Tamagotchi: Pre-Game");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 600);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JLabel lblNumberOfPlayers = new JLabel("Number of players:");
        lblNumberOfPlayers.setFont(new Font("Tahoma", Font.PLAIN, 14));

        final JComboBox comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"3", "2", "1"}));

        JLabel lblPlayerNamesopt = new JLabel("Player names: (Optional)");
        lblPlayerNamesopt.setFont(new Font("Tahoma", Font.PLAIN, 14));

        JLabel lblNumberOfPets = new JLabel("Number of Pets:");
        lblNumberOfPets.setFont(new Font("Tahoma", Font.PLAIN, 14));

        textField = new JTextField();
        textField.setColumns(10);

        JComboBox comboBox_1 = new JComboBox();
        comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"3", "2", "1"}));

        textField_1 = new JTextField();
        textField_1.setColumns(10);

        final JComboBox comboBox_2 = new JComboBox();
        comboBox_2.setModel(new DefaultComboBoxModel(new String[] {"3", "2", "1"}));

        textField_2 = new JTextField();
        textField_2.setColumns(10);

        comboBox_3 = new JComboBox();
        comboBox_3.setModel(new DefaultComboBoxModel(new String[] {"3", "2", "1"}));

        JLabel lblPetNamesoptional = new JLabel("Pet names: (Optional)");
        lblPetNamesoptional.setFont(new Font("Tahoma", Font.PLAIN, 14));

        JLabel lblPet = new JLabel("Pet 1");

        lblPet_1 = new JLabel("Pet 2");

        lblPet_2 = new JLabel("Pet 3");

        JLabel lblPlayer = new JLabel("Player 1");

        lblPlayer_1 = new JLabel("Player 2");

        lblPlayer_2 = new JLabel("Player 3");

        textField_3 = new JTextField();
        textField_3.setColumns(10);

        textField_4 = new JTextField();
        textField_4.setColumns(10);

        textField_5 = new JTextField();
        textField_5.setColumns(10);

        textField_6 = new JTextField();
        textField_6.setColumns(10);

        textField_7 = new JTextField();
        textField_7.setColumns(10);

        textField_8 = new JTextField();
        textField_8.setColumns(10);

        textField_9 = new JTextField();
        textField_9.setColumns(10);

        textField_10 = new JTextField();
        textField_10.setColumns(10);

        textField_11 = new JTextField();
        textField_11.setColumns(10);

        JButton btnCancel = new JButton("Cancel");
        btnCancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        }); 

        final JButton btnOk = new JButton("OK");
        btnOk.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        }); 
        JLabel lblPetSpecies = new JLabel("Pet species:");
        lblPetSpecies.setFont(new Font("Tahoma", Font.PLAIN, 14));

        JLabel label_3 = new JLabel("Player 1");

        label = new JLabel("Player 2");

        label_1 = new JLabel("Player 3");

        comboBox_4 = new JComboBox();
        comboBox_4.setModel(new DefaultComboBoxModel(new String[] {"Mametchi", "Kuchipatchi", "Tarakotchi"}));

        comboBox_5 = new JComboBox();
        comboBox_5.setModel(new DefaultComboBoxModel(new String[] {"Kuchipatchi", "Mametchi", "Tarakotchi"}));

        comboBox_6 = new JComboBox();
        comboBox_6.setModel(new DefaultComboBoxModel(new String[] {"Tarakotchi", "Mametchi", "Kuchipatchi"}));

        comboBox_7 = new JComboBox();
        comboBox_7.setModel(new DefaultComboBoxModel(new String[] {"Mametchi", "Kuchipatchi", "Tarakotchi"}));

        comboBox_8 = new JComboBox();
        comboBox_8.setModel(new DefaultComboBoxModel(new String[] {"Kuchipatchi", "Mametchi", "Tarakotchi"}));

        comboBox_9 = new JComboBox();
        comboBox_9.setModel(new DefaultComboBoxModel(new String[] {"Tarakotchi", "Mametchi", "Kuchipatchi"}));

        comboBox_10 = new JComboBox();
        comboBox_10.setModel(new DefaultComboBoxModel(new String[] {"Mametchi", "Kuchipatchi", "Tarakotchi"}));

        comboBox_11 = new JComboBox();
        comboBox_11.setModel(new DefaultComboBoxModel(new String[] {"Kuchipatchi", "Mametchi", "Tarakotchi"}));

        comboBox_12 = new JComboBox();
        comboBox_12.setModel(new DefaultComboBoxModel(new String[] {"Tarakotchi", "Mametchi", "Kuchipatchi"}));

        JLabel lblPet_3 = new JLabel("Pet 1");

        lblPet_4 = new JLabel("Pet 2");

        lblPet_5 = new JLabel("Pet 3");

        JLabel lblDaysToPlay = new JLabel("Days to play:");
        lblDaysToPlay.setFont(new Font("Tahoma", Font.PLAIN, 14));

        JComboBox comboBox_13 = new JComboBox();
        comboBox_13.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "5", "10", "15", "20", "30", "40", "50"}));



        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGap(267)
                    .addComponent(lblPet_3)
                    .addGap(69)
                    .addComponent(lblPet_4)
                    .addGap(88)
                    .addComponent(lblPet_5)
                    .addContainerGap(285, Short.MAX_VALUE))
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGap(272)
                    .addComponent(lblPet)
                    .addContainerGap(487, Short.MAX_VALUE))
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGap(185)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                            .addComponent(lblDaysToPlay)
                            .addPreferredGap(ComponentPlacement.UNRELATED)
                            .addComponent(comboBox_13, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(ComponentPlacement.RELATED, 199, Short.MAX_VALUE)
                            .addComponent(btnOk, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(ComponentPlacement.RELATED)
                            .addComponent(btnCancel)
                            .addGap(138))
                        .addGroup(gl_contentPane.createSequentialGroup()
                            .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                .addComponent(lblPetSpecies)
                                .addGroup(gl_contentPane.createSequentialGroup()
                                    .addComponent(lblPlayer_1)
                                    .addGap(18)
                                    .addComponent(textField_6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(ComponentPlacement.RELATED)
                                    .addComponent(textField_7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(ComponentPlacement.RELATED)
                                    .addComponent(textField_8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGroup(gl_contentPane.createSequentialGroup()
                                    .addComponent(lblPlayer)
                                    .addGap(18)
                                    .addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(ComponentPlacement.RELATED)
                                    .addComponent(textField_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(ComponentPlacement.RELATED)
                                    .addComponent(textField_5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGroup(gl_contentPane.createSequentialGroup()
                                    .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                            .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                                .addComponent(lblPlayerNamesopt)
                                                .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addComponent(lblPetNamesoptional))
                                            .addGap(18))
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                            .addComponent(lblNumberOfPlayers)
                                            .addPreferredGap(ComponentPlacement.UNRELATED)
                                            .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                        .addComponent(comboBox_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblNumberOfPets)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                            .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                                                .addComponent(lblPet_1)
                                                .addComponent(comboBox_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                            .addGap(69)
                                            .addComponent(lblPet_2))))
                                .addGroup(gl_contentPane.createSequentialGroup()
                                    .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                            .addComponent(lblPlayer_2)
                                            .addGap(18)
                                            .addComponent(textField_9, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                            .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                                .addComponent(label_3, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)
                                                .addComponent(label_1, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE))
                                            .addGap(26)
                                            .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                                .addComponent(comboBox_7, 0, 90, Short.MAX_VALUE)
                                                .addComponent(comboBox_4, 0, 90, Short.MAX_VALUE)
                                                .addComponent(comboBox_10, 0, 90, Short.MAX_VALUE))))
                                    .addPreferredGap(ComponentPlacement.RELATED)
                                    .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                            .addComponent(textField_10, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(ComponentPlacement.RELATED)
                                            .addComponent(textField_11, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                            .addGap(8)
                                            .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                                .addComponent(comboBox_5, 0, 95, Short.MAX_VALUE)
                                                .addComponent(comboBox_8, 0, 95, Short.MAX_VALUE)
                                                .addComponent(comboBox_11, 0, 95, Short.MAX_VALUE))
                                            .addGap(18)
                                            .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
                                                .addComponent(comboBox_6, 0, 96, Short.MAX_VALUE)
                                                .addComponent(comboBox_9, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(comboBox_12, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                                .addGroup(gl_contentPane.createSequentialGroup()
                                    .addComponent(label, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(ComponentPlacement.RELATED, 339, Short.MAX_VALUE)))
                            .addGap(221))))
        );
        gl_contentPane.setVerticalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGap(22)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                            .addGap(35)
                            .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                .addComponent(lblPlayerNamesopt)
                                .addComponent(lblNumberOfPets)))
                        .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                            .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblNumberOfPlayers)))
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(comboBox_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(comboBox_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18)
                    .addComponent(lblPetNamesoptional)
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                        .addComponent(lblPet)
                        .addComponent(lblPet_2)
                        .addComponent(lblPet_1))
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                        .addComponent(lblPlayer)
                        .addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField_5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                        .addComponent(lblPlayer_1)
                        .addComponent(textField_6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField_7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField_8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                        .addComponent(lblPlayer_2)
                        .addComponent(textField_9, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField_10, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField_11, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                        .addComponent(lblPet_3)
                        .addGroup(gl_contentPane.createSequentialGroup()
                            .addComponent(lblPetSpecies)
                            .addGap(9)
                            .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                .addComponent(lblPet_4)
                                .addComponent(lblPet_5))))
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                        .addComponent(label_3)
                        .addComponent(comboBox_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(comboBox_5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(comboBox_6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                        .addComponent(label)
                        .addComponent(comboBox_7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(comboBox_8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(comboBox_9, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                        .addComponent(label_1)
                        .addComponent(comboBox_10, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(comboBox_11, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(comboBox_12, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(42)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                        .addComponent(lblDaysToPlay)
                        .addComponent(comboBox_13, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnOk)
                        .addComponent(btnCancel))
                    .addGap(52))
        );
        contentPane.setLayout(gl_contentPane);
        }

    public static ArrayList<String> PetNames() {
        System.out.println(textField_3.getText().toString());
        petTexts.add(textField_3.getText().toString());
        petTexts.add(textField_4.getText());
        petTexts.add(textField_5.getText());
        petTexts.add(textField_6.getText());
        petTexts.add(textField_7.getText());
        petTexts.add(textField_8.getText());
        petTexts.add(textField_9.getText());
        petTexts.add(textField_10.getText());
        petTexts.add(textField_11.getText());
        return petTexts;
    }

    public static ArrayList<String> playerNames() {
        playerTexts.add(textField.getText());
        playerTexts.add(textField_1.getText());
        playerTexts.add(textField_2.getText());
        return playerTexts;
    }
    public static ArrayList<String> speciesNames() {
        System.out.println("hello");
        speciesTexts.add(comboBox_4.getSelectedItem().toString());
        speciesTexts.add(comboBox_5.getSelectedItem().toString());
        speciesTexts.add(comboBox_6.getSelectedItem().toString());
        speciesTexts.add(comboBox_7.getSelectedItem().toString());
        speciesTexts.add(comboBox_8.getSelectedItem().toString());
        speciesTexts.add(comboBox_9.getSelectedItem().toString());
        speciesTexts.add(comboBox_10.getSelectedItem().toString());
        speciesTexts.add(comboBox_11.getSelectedItem().toString());
        return speciesTexts;
    }
    public void disablePlayer23() {
        disablePlayer2();
        disablePlayer3();
    }

    public void disablePlayer2() {
        textField_1.setEnabled(false);
        comboBox_2.setEnabled(false);
        textField_6.setEnabled(false);
        textField_7.setEnabled(false);
        textField_8.setEnabled(false);
        lblPlayer_1.setEnabled(false);
        comboBox_7.setEnabled(false);
        comboBox_8.setEnabled(false);
        comboBox_9.setEnabled(false);
        label.setEnabled(false);
    }

    public void enablePlayer2() {
        textField_1.setEnabled(true);
        comboBox_2.setEnabled(true);
        textField_6.setEnabled(true);
        textField_7.setEnabled(true);
        textField_8.setEnabled(true);
        lblPlayer_1.setEnabled(true);
        comboBox_7.setEnabled(true);
        comboBox_8.setEnabled(true);
        comboBox_9.setEnabled(true);
        label.setEnabled(true);
    }

    public void disablePlayer3() {
        textField_2.setEnabled(false);
        comboBox_3.setEnabled(false);
        textField_9.setEnabled(false);
        textField_10.setEnabled(false);
        textField_11.setEnabled(false);
        lblPlayer_2.setEnabled(false);
        comboBox_10.setEnabled(false);
        comboBox_11.setEnabled(false);
        comboBox_12.setEnabled(false);
        label_1.setEnabled(false);
    }

    public void enablePlayer3() {
        textField_2.setEnabled(true);
        comboBox_3.setEnabled(true);
        textField_9.setEnabled(true);
        textField_10.setEnabled(true);
        textField_11.setEnabled(true);
        lblPlayer_2.setEnabled(true);
        comboBox_10.setEnabled(true);
        comboBox_11.setEnabled(true);
        comboBox_12.setEnabled(true);
        label_1.setEnabled(true);
    }

    public void enablePlayer23() {
        enablePlayer2();
        enablePlayer3();    
    }

    public void disableP2Pet23() {
        disableP2Pet2();
        disableP2Pet3();
    }

    public void disableP2Pet2() {
        textField_7.setEnabled(false);
        comboBox_8.setEnabled(false);
    }

    public void disableP2Pet3() {
        textField_8.setEnabled(false);
        comboBox_9.setEnabled(false);
    }

    public void enableP2Pet2() {
        textField_7.setEnabled(true);
        comboBox_8.setEnabled(true);
    }

    public void enableP2Pet3() {
        textField_8.setEnabled(true);
        comboBox_9.setEnabled(true);
    }

    public void enableP2Pet23() {
        enableP2Pet2();
        enableP2Pet3();
    }
}

Upvotes: 0

Views: 3780

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347184

  1. Don't use static in this way. It smacks of poor design and will only restrict what you can achieve in the long run.
  2. You are shadowing your variables...

For example, you declare a class level field...

private static JComboBox comboBox_2;

But in your constructor you re declare it as a local variable...

final JComboBox comboBox_2 = new JComboBox();

Meaning that the class level field is still null

Upvotes: 1

acdcjunior
acdcjunior

Reputation: 135752

All your fields are initialized in GUI_preGame's constructor.

As PetNames() is a static method, though, you can access it without having to call GUI_preGame's constructor first.

When you do that, as all those textField_* objects are not initialized, they will be null and any attempt to execute a method from them will throw a NullPointerException.

Solution: Make PetNames() non-static so it is attached to a GUI_preGame instance (that will only exist if the GUI_preGame's constructor is called), or, better yet, make PetNames() and all your other static methods and fields non-static, as your code right now is making a bad use of the static feature:

public static ArrayList<String> PetNames() { //<-- remove "static" from this line
    System.out.println(textField_3.getText().toString());
    petTexts.add(textField_3.getText().toString());
    petTexts.add(textField_4.getText());
    petTexts.add(textField_5.getText());
    petTexts.add(textField_6.getText());
    petTexts.add(textField_7.getText());
    petTexts.add(textField_8.getText());
    petTexts.add(textField_9.getText());
    petTexts.add(textField_10.getText());
    petTexts.add(textField_11.getText());
    return petTexts;
}

Upvotes: 1

Related Questions