好的,所以我有一些带有一些textFields和组合框的GUI。每当我尝试我得到文本textField.getText()
和comboBox.getSelectedItem()
时,我得到一个空指针异常。
即使我的comboBox默认为集合String
或我在textField中有文本。
我运行GUI_preGame
类并在框中输入内容,然后调用getText()
。
很抱歉代码太乱了(部分原因是使用了窗口构建器组布局)
我使用GUI_preGame.PetNames();
从另一个类调用此类
错误在于这一行:498(我输入的打印语句只是打印它!)
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();
}
}
答案 0 :(得分:1)
所有字段都在GUI_preGame
的构造函数中初始化。
由于PetNames()
是一种static
方法,您无需先调用GUI_preGame
的构造函数即可访问它。
当你这样做时,由于所有textField_*
个对象都没有初始化,它们将是null
,任何从它们执行方法的尝试都将抛出NullPointerException
。
解决方案:将PetNames()
设为非static
,以便将其附加到GUI_preGame
实例(仅当GUI_preGame
'时才会存在s构造函数被调用),或者更好的是,使PetNames()
和所有其他 static
方法和字段非静态,因为您的代码现在使用不当static
功能:
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;
}
答案 1 :(得分:1)
static
。它设计很糟糕,只会限制你长期可以实现的目标。例如,您声明了一个类级别字段...
private static JComboBox comboBox_2;
但是在你的构造函数中,你将它声明为局部变量......
final JComboBox comboBox_2 = new JComboBox();
意味着班级字段仍为null