I am currently working on a Hangman GUI as a HW assignment and I have all the code done (I think) but, I am getting a nullpointerexception and cannot figure out for the life of me where it is originating and it’s making me rather furious. I need a second set of eyes to help me figure out where I made a mistake and what I need to do to correct it. Thanks in advance!
The NPE is occuring at:
alphabet = "abcdefghijklmnopqrxtuvwxyz";
numLetters = 26;
for (int count = 0; count < numLetters; count++) {
letterChoice[count] = new JButton(Character.toString(alphabet
.charAt(count)));
letterChoice[count].addActionListener(new CharacterListener(
alphabet.charAt(count)));
letterChoice[count].setMnemonic(65 + count);
add(letterChoice[count]);
}
And here is all my code.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
public class HangmanPanel extends JPanel {
private JLabel imageLabel, numberLetters, gameOver, youWin;
private JLabel[] spaces;
private ImageIcon[] images;
private JButton exitProgram, newGame, nextImage;
private JButton[] letterChoice;
private int imageNumber, letterNumber, numLetters, guesses;
private WordList wordRand;
private String word, alphabet;
public HangmanPanel() {
newGame = new JButton("New Game");
newGame.setEnabled(true);
newGame.setToolTipText("Press to restart game.");
newGame.addActionListener(new NewGame());
exitProgram = new JButton("Exit");
exitProgram.setEnabled(true);
exitProgram.setToolTipText("Press to close the program.");
exitProgram.addActionListener(new ExitGame());
wordRand = new WordList();
word = wordRand.getWord();
images = new ImageIcon[8];
// Populating the array
{
images[0] = new ImageIcon("hangman0.png");
images[1] = new ImageIcon("hangman1.png");
images[2] = new ImageIcon("hangman2.png");
images[3] = new ImageIcon("hangman3.png");
images[4] = new ImageIcon("hangman4.png");
images[5] = new ImageIcon("hangman5.png");
images[6] = new ImageIcon("hangman6.png");
images[7] = new ImageIcon("hangman7.png");
}
setBackground(Color.white);
imageLabel = new JLabel(images[imageNumber]);
imageNumber++;
add(imageLabel);
alphabet = "abcdefghijklmnopqrxtuvwxyz";
numLetters = 26;
for (int count = 0; count < numLetters; count++) {
letterChoice[count] = new JButton(Character.toString(alphabet
.charAt(count)));
letterChoice[count].addActionListener(new CharacterListener(
alphabet.charAt(count)));
letterChoice[count].setMnemonic(65 + count);
add(letterChoice[count]);
}
spaces = new JLabel[word.length()];
while (letterNumber < spaces.length) {
numberLetters = new JLabel("___");
add(numberLetters);
letterNumber++;
}
add(nextImage);
add(newGame);
add(exitProgram);
}
private class NewGame implements ActionListener {
public void actionPerformed(ActionEvent event) {
imageLabel.setIcon(images[0]);
imageNumber = 0;
imageNumber++;
imageLabel.repaint();
}
}
private class ExitGame implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
private class CharacterListener implements ActionListener {
public CharacterListener(char charAt) {
}
public void actionPerformed(ActionEvent e) {
while (guesses < images.length) {
int count = 0;
while (count < word.charAt(count)) {
if (letterChoice[count].equals(word.charAt(count))) {
spaces[count] = new JLabel("" + letterChoice[count]
+ "");
count++;
} else
imageLabel.setIcon(images[imageNumber]);
imageNumber++;
imageLabel.repaint();
guesses++;
}
if (guesses == 7) {
gameOver = new JLabel(
"You lose! Press New Game to try again!");
add(gameOver);
} else
youWin = new JLabel(
"You win! Press New Game to play again or press Exit to remain Victorious!");
add(youWin);
}
}
}
}
The NullPointerException should tell you which line is causing the problem. Could you please post the exception and we’ll try to help you figure out why its occurring at the particular line.
Without this information, there are a few possible errors..
1st possible error…
You’re not creating the letterChoice array – ie you’re missing the following somewhere before the alphabet…
2nd possible error…
You’re not creating the
nextImage– ie you’re missing the following…3rd possible error…
The reason: if you’re doing a command like
word.charAt(count), you should make sure that count is less than the length of the word first. Change the line to…4th possible error…
Your list of letters doesn’t have an ‘s’ in it, but rather it has 2 ‘s’ characters.