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

260
Views
Errores del juego del ahorcado

Aquí está mi programa hasta ahora:

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

Mi salida debería ser algo como esto:

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

Sin embargo, recibo los siguientes errores:

 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

Está redefiniendo la palabra variable y el progreso en su constructor. Simplemente debe usarlos sin declararlos como nuevas variables porque ya están definidos. Actualmente los está definiendo localmente, el constructor funciona pero usa esas variables locales definidas y no las variables de palabra y progreso de su objeto, por lo tanto, cuando deje el alcance y llame a display (), usará la matriz de progreso de su objeto que nunca se inicializó.

Cámbielo a lo siguiente para que no esté redefiniendo la palabra de las variables y progrese así

 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!