hi i am gettin a null pointer exception with the code bbelow. it compiles fine, however when clicking on the playGame button (labelled play) i get null pointer eexception errors in the action listener apparently to do with this line "int amountGames = Integer.parseInt(numGames_input.getText());". Can anyone also give me an idea on how i could line up my boxes/ labels nicer? Thanks!
public static void main(String[] args) { JFrame frame = new GameFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(380, 520); frame.setResizable(false); frame.show(); }
public GameFrame() { super("Rock Paper Scissors"); titleText = new JLabel("ROCK PAPER SCISSORS", JLabel.CENTER); getContentPane().add(titleText, BorderLayout.CENTER);
public JPanel createGameOptions() { JLabel numHum = new JLabel("Please insert desired number of human players: ", JLabel.LEFT); JTextField numHum_input = new JTextField("0", 5); JLabel numCom = new JLabel("Please insert desired number of computer players: ", JLabel.LEFT); JTextField numCom_input = new JTextField("0", 5); JTextField numGames_input = new JTextField("1", 5); JLabel numGames = new JLabel("Please insert desired number of games to play: ", JLabel.LEFT); JButton playGame = new JButton("Play");
public JPanel createPlayerChoices() { JLabel choiceInstruct = new JLabel("Player Select Choice: ", JLabel.LEFT);
JRadioButton radioScissors = new JRadioButton("Scissors"); JRadioButton radioPaper = new JRadioButton("Paper"); JRadioButton radioRock = new JRadioButton("Rock");
ButtonGroup group = new ButtonGroup(); group.add(radioScissors); group.add(radioPaper); group.add(radioRock);
JButton confirmChoice = new JButton("Confirm Choice");
public void actionPerformed(ActionEvent event) { int amountGames = Integer.parseInt(numGames_input.getText()); int amountHumans = Integer.parseInt(numHum_input.getText()); int amountComputers = Integer.parseInt(numCom_input.getText()); }
public JPanel createGameOptions()
{
JLabel numHum = new JLabel("Please insert desired number of human players: ", JLabel.LEFT);
JTextField numHum_input = new JTextField("0", 5);
JLabel numCom = new JLabel("Please insert desired number of computer players: ", JLabel.LEFT);
JTextField numCom_input = new JTextField("0", 5);
JTextField numGames_input = new JTextField("1", 5);
JLabel numGames = new JLabel("Please insert desired number of games to play: ", JLabel.LEFT);
JButton playGame = new JButton("Play");
Here you are setting values for the LOCAL VARIABLES numHum_input, numCom_input, ... You never actually initialise your INSTANCE VARIABLES numHum_input, numCom_input, ... (the ones declared straight after "class GameFrame). Therefore when you code calls numHum_input.getText(), it is referring to the INSTANCE VARIABLE numHum_input, which has never been initialised.
With regard to a better layout, you need to read up on LayoutManagers. A quick and simple way is to set the LayoutManager to null and use absolute positioning.
you're right. I knew it was something to do with variables not being initialised but i thought initialising them locally was all you had to do. Thanks!