Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda