Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7788497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:01:30+00:00 2026-06-01T21:01:30+00:00

I am currently working on a Hangman GUI as a HW assignment and I

  • 0

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

        }

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:01:31+00:00Added an answer on June 1, 2026 at 9:01 pm

    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…

    letterChoice = new JButton[26];
    

    2nd possible error…
    You’re not creating the nextImage – ie you’re missing the following…

    newImage = new JButton("Blah");
    

    3rd possible error…

        public void actionPerformed(ActionEvent e) {
            while (guesses < images.length) {
                int count = 0;
                while (count < word.charAt(count)) { // probably here
                    if (letterChoice[count].equals(word.charAt(count))) { // maybe here too
                        spaces[count] = new JLabel("" + letterChoice[count]
                                + "");
                        count++;
                    } else
                        imageLabel.setIcon(images[imageNumber]);
                    imageNumber++;
                    imageLabel.repaint();
                    guesses++;
    
                }
    

    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…

    while (count < word.length && count < word.charAt(count)) {
    

    4th possible error…
    Your list of letters doesn’t have an ‘s’ in it, but rather it has 2 ‘s’ characters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently working on trying to figure out asynchronous submission in ColdFusion. I always have
Currently working on a site built in Django and i'm getting an issue when
I`m currently working on a script, and I have the following situation. function somnicefunction()
I'm currently working on an alternative way to view the threads and messages. But
I am currently working on a registration based website, and I have the server
Currently working with Oracle, but will also need a solution for MS SQL. I
Im currently working with socket programming, but i am having problems connecting to my
Currently working on a project in MVC-3. Trying to put the following code in
Im currently working on a RTX/Janrain integration with bbPress, but im stuck with a
Iam currently working on a iPad project where i build my code on top

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.