I'm currently working on a college Hangman project to build a GUI to ply an external hangman game, but I keep recieving a NullPointerException error. Here is the code for the GUI
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ImageIcon;
public class HangmanFrame extends JFrame
{
private JTextField wordField;
private JTextField livesField;
private static final int FRAME_WIDTH = 650;
private static final int FRAME_HEIGHT = 550;
private JButton newButton;
private JButton quitButton;
private JButton[] alphaButton;
private char[] names;
private JPanel containerPanel;
private JPanel northPanel;
private JPanel southPanel;
private JPanel westPanel;
private JPanel centrePanel;
private JLabel imageLabel;
private HangmanGame game;
private WordGenerator myWord;
private Font buttonFont = new Font("Bradley Hand ITC",Font.BOLD, 18);
private Font largeFont = new Font("Bradley Hand ITC",Font.BOLD, 24);
public HangmanFrame()
{
setSize(FRAME_WIDTH, FRAME_HEIGHT);
containerPanel = new JPanel();
WordGenerator myWord = new WordGenerator();
HangmanGame game = new HangmanGame(myWord.getWord());
createWordField();
createImagePanel();
createLetterPanel();
createButtonPanel();
createContainerPanel();
createMenuBar();
}
public void createMenuBar()
{
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
JMenu levelMenu = new JMenu("Level");
menuBar.add(fileMenu);
menuBar.add(levelMenu);
JMenuItem exitAction = new JMenuItem("Exit");
JRadioButtonMenuItem EasyRB = new JRadioButtonMenuItem("Easy");
JRadioButtonMenuItem HardRB = new JRadioButtonMenuItem("Hard");
ButtonGroup bg = new ButtonGroup();
bg.add(EasyRB);
bg.add(HardRB);
levelMenu.add(EasyRB);
levelMenu.add(HardRB);
fileMenu.add(exitAction);
}
public JPanel createContainerPanel()
{
containerPanel.setLayout(new BorderLayout());
containerPanel.add(createWordField(), BorderLayout.NORTH);
containerPanel.add(createImagePanel(), BorderLayout.WEST);
containerPanel.add(createLetterPanel(), BorderLayout.CENTER);
containerPanel.add(createButtonPanel(), BorderLayout.SOUTH);
add(containerPanel);
return containerPanel;
}
public JTextField createWordField()
{
wordField = new JTextField(10);
wordField.setHorizontalAlignment(JTextField.CENTER);
wordField.setEditable(false);
wordField.setBackground(Color.black);
wordField.setForeground(Color.white);
wordField.setBorder(null);
JPanel northPanel = new JPanel();
northPanel.add(wordField);
northPanel.setBackground(Color.black);
wordField.setFont(largeFont);
return wordField;
}
public Image getImage(String path)
{
Image image = null;
return image;
}
public JPanel createImagePanel()
{
JPanel westPanel = new JPanel(new BorderLayout());
ImageIcon image = new ImageIcon("background.jpg");
JLabel imageLabel = new JLabel(image);
westPanel.setBackground(Color.black);
JTextField livesField = new JTextField();
livesField.setBackground(Color.black);
livesField.setForeground(Color.white);
livesField.setFont(buttonFont);
livesField.setBorder(null);
westPanel.add(imageLabel,BorderLayout.CENTER);
westPanel.add(livesField,BorderLayout.SOUTH);
return westPanel;
}
public JPanel createLetterPanel()
{
JPanel centrePanel = new JPanel();
centrePanel.setLayout(new GridLayout(5, 5));
centrePanel.setBackground(Color.black);
final JButton alphaButton[]=new JButton[26];
String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i <alphaButton.length; i++)
{
String letter = "" + alpha.charAt(i);
alphaButton[i]= new JButton(letter);
centrePanel.add(alphaButton[i]);
alphaButton[i].setOpaque(false);
alphaButton[i].setContentAreaFilled(false);
alphaButton[i].setBorderPainted(false);
alphaButton[i].setEnabled(false);
alphaButton[i].setFont(buttonFont);
alphaButton[i].setForeground(Color.white);
ActionListener listener = new ButtonListener();
alphaButton[i].addActionListener(listener);
}
return centrePanel;
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String label = e.getActionCommand();
JButton button = (JButton)e.getSource();
game.guessLetter(label.charAt(0));
wordField.setText(game.showDashes());
livesField.setText(" "+game.getLives());
button.setEnabled(false);
button.setVisible(false);
}
}
public JPanel createButtonPanel()
{
JPanel southPanel = new JPanel();
southPanel.setLayout(new GridLayout(1, 2));
southPanel.setBackground(Color.black);
newButton = new JButton("New Game");
newButton.setOpaque(false);
newButton.setContentAreaFilled(false);
newButton.setBorderPainted(false);
newButton.setFont(buttonFont);
newButton.setForeground(Color.white);
ActionListener newListener = new NewButtonListener();
newButton.addActionListener(newListener);
quitButton = new JButton("Quit");
quitButton.setOpaque(false);
quitButton.setContentAreaFilled(false);
quitButton.setBorderPainted(false);;
quitButton.setFont(buttonFont);
quitButton.setForeground(Color.white);
ActionListener quitListener = new QuitButtonListener();
quitButton.addActionListener(quitListener);
southPanel.add(newButton);
southPanel.add(quitButton);
return southPanel;
}
class NewButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent f)
{
wordField.setText(game.showDashes());
livesField.setText(" "+game.getLives());
for (int i=0; i<26; i++)
{
alphaButton[i].setEnabled(true);
}
}
}
class QuitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent g)
{
System.exit(0);
}
}
}
Here is the external game I am tryin to import:
public class HangmanGame
{
private String secretWord;
private char[] dashes;
private int lives;
//constructor
//sets secretWord
//creates and initialises dashes with correct no of dashes
//sets lives to default value 8
public HangmanGame(String wordIn)
{
secretWord = wordIn;
dashes = new char[secretWord.length()];
fillDashes();
lives = 8; //default
}
//checks if letter guessed is in secretword
//if it is - puts letter in correct location in dashes[]
//else lose a life
public void guessLetter(char letterIn)
{
boolean found = false;
for(int i = 0; i<secretWord.length(); i++)
{
if(letterIn == secretWord.charAt(i)){
dashes[i] = letterIn;
found = true;
}
}
if(!found)
lives--;
}
//returns true when game over - correct word guessed or 0 lives left
//otherwise returns false
public boolean gameOver()
{
if(secretWord.equalsIgnoreCase(showDashes()) || lives == 0)
return true;
else
return false;
//return secretWord.equalsIgnoreCase(showDashes()) || lives == 0;
}
//returns the secretword
public String showSecretWord()
{
return secretWord;
}
//method to show the chars in dashes -
//returns a string made up of either dashes or correct letter
public String showDashes()
{
String s ="";
for(int i = 0; i< dashes.length; i++)
s +=dashes[i];
return s;
}
//method initialises dashes[] with dashes at start of game
public void fillDashes()
{
for(int i = 0; i< dashes.length; i++)
dashes[i]='-';
}
//returns the num of lives left
public int getLives()
{
return lives;
}
//sets the number of lives
public void setLives(int livesIn)
{
lives = livesIn;
}
}
I am using a seperate viewer to view the game, but each time I click on new game i am getting the following error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at HangmanFrame$NewButtonListener.actionPerformed(HangmanFrame.java:246)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6382)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
at java.awt.Component.processEvent(Component.java:6147)
at java.awt.Container.processEvent(Container.java:2083)
at java.awt.Component.dispatchEventImpl(Component.java:4744)
at java.awt.Container.dispatchEventImpl(Container.java:2141)
at java.awt.Component.dispatchEvent(Component.java:4572)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)
at java.awt.Container.dispatchEventImpl(Container.java:2127)
at java.awt.Window.dispatchEventImpl(Window.java:2489)
at java.awt.Component.dispatchEvent(Component.java:4572)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:710)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:669)
at java.awt.EventQueue$2.run(EventQueue.java:667)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:683)
at java.awt.EventQueue$3.run(EventQueue.java:681)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:680)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
I am really stuck on this, any help would be much appreciated, thanks so much in advance