Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
Hangman game errors

Here is my program so far:

import java.util.Arrays;

public class HangmanWord {

    private String[] possibleWords = {"laptop", "college", "programing"};
    private String word;
    private char[] progress;
    private int wrongCount = 0;
public HangmanWord() {
    int randomPossibleWord = (int) (Math.random() * possibleWords.length-1);
  String word = possibleWords[randomPossibleWord];
  char[] progress = new char[word.length()];
  Arrays.fill(progress,'-');
  }
public void display() {
    System.out.print(progress);
    System.out.println();
}
public boolean guess(char c) {
    boolean matchFound = false;
    for (int i = 0; i < word.length(); i++ ) {
        if (word.charAt(i) == c ) {
            progress[i] = c;
            matchFound = true;
        }
    }
    return false;

}

public boolean isSolved() {
    for (int i = 0; i < progress.length; i++ ) {
        if(progress[i] == '-'){
            return false;
        }
    }
    return true;

}
public int getWrongCount() {
    return wrongCount;  
}
public String getWord() {
    return word;

}

}

public class Hangman {

    public static void main(String[] args) {
int MAX_INCORRECT = 5;
System.out.println("Welcome to Hangman.");
HangmanWord wordObj = new HangmanWord();
System.out.print("Here is your word: ");
wordObj.display();
    }

}

My output should look something like this:

Welcome to Hangman.
Here is your word: ------

However, I am getting the following errors:

Welcome to Hangman.
Here is your word: Exception in thread "main" java.lang.NullPointerException
    at java.io.Writer.write(Writer.java:127)
    at java.io.PrintStream.write(PrintStream.java:503)
    at java.io.PrintStream.print(PrintStream.java:653)
    at HangmanWord.display(HangmanWord.java:16)
    at Hangman.main(Hangman.java:9)
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You are redefining the variable word and progress in your constructor. You should simply use them without declaring them as a new variables because they're already defined. Currently you are defining them locally, the constructor works but uses those local defined variables and not your object's word and progress variables, therefore when you leave scope and call display() it will use your object's progress array which was never actually initialized.

Change it to the following so you aren't redefining the variables word and progress like so

public HangmanWord() {
    int randomPossibleWord = (int) (Math.random() * possibleWords.length-1);
    word = possibleWords[randomPossibleWord];
    progress = new char[word.length()];
    Arrays.fill(progress,'-');
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!