The Artima Developer Community
Sponsored Link

Java Answers Forum
null pointer exception error

2 replies on 1 page. Most recent reply: Aug 13, 2003 2:12 AM by Matheo

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Matheo

Posts: 2
Nickname: matttai
Registered: Aug, 2003

null pointer exception error Posted: Aug 13, 2003 1:21 AM
Reply to this message Reply
Advertisement
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!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class GameFrame extends JFrame implements ActionListener
{

private JLabel titleText;

public JTextField numGames_input;
private JTextField numCom_input;
private JTextField numHum_input;
private JLabel numCom;
private JLabel numHum;
private JLabel numGames;
private JButton playGame;

private JLabel choiceInstruct;
private JRadioButton radioScissors;
private JRadioButton radioPaper;
private JRadioButton radioRock;
private JButton confirmChoice;

private JTextArea resultsWindow;

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);

createControlPanel();
pack();
}

public void createControlPanel()
{
JPanel gameOptions = createGameOptions();
JPanel playerChoices = createPlayerChoices();
JPanel gameResults = createGameResults();

JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(3,1));
controlPanel.add(gameOptions);
controlPanel.add(playerChoices);
controlPanel.add(gameResults);

getContentPane().add(controlPanel, BorderLayout.SOUTH);

}

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");

JPanel panel = new JPanel();

panel.add(numHum);
panel.add(numHum_input);
panel.add(numCom);
panel.add(numCom_input);
panel.add(numGames);
panel.add(numGames_input);
panel.add(playGame);

playGame.addActionListener(this);

panel.setBorder(new TitledBorder(new EtchedBorder(), "Options"));

return panel;
}

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");

JPanel panel = new JPanel();

panel.add(choiceInstruct);
panel.add(radioScissors);
panel.add(radioPaper);
panel.add(radioRock);
panel.add(confirmChoice);

panel.setBorder(new TitledBorder(new EtchedBorder(), "Choices"));

return panel;
}

public JPanel createGameResults()
{
JTextArea resultsWindow = new JTextArea(7,30);
resultsWindow.setLineWrap(true);
resultsWindow.setWrapStyleWord(true);
JScrollPane scroll = new JScrollPane(resultsWindow,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

JPanel panel = new JPanel();

panel.add(scroll);

panel.setBorder(new TitledBorder(new EtchedBorder(), "Results"));

return panel;
}

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());
}


David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: null pointer exception error Posted: Aug 13, 2003 1:51 AM
Reply to this message Reply
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.

Matheo

Posts: 2
Nickname: matttai
Registered: Aug, 2003

Re: null pointer exception error Posted: Aug 13, 2003 2:12 AM
Reply to this message Reply
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!

Flat View: This topic has 2 replies on 1 page
Topic: jbutton problem Previous Topic   Next Topic Topic: JAVA 3D

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use