Search code examples
javaandroidandroid-activitymethodsdialog

Unable to run Activity method in Dialog Fragment


I am making a hangman game in Android Studio using Java. I have a button "Guess" which opens a dialog that includes an edit text and two buttons. One of the buttons is to confirm the guess so it gets the text entered (guessedWord) and compares it to the hangman word (wordToGuess). If it's wrong it adds an image and if it's correct the user wins the game. The method to compare the words is in my activity (GameActivity.java) but the button press is in my Dialog Fragment (GuessWord.java). I am trying to call the method runGuessWord() in the Dialog Fragment but I get an error when I click on the confirm button:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.hangman.GameActivity.guessWord()' on a null object reference

I tried using a getter and setter to send the inputted text. Code below:

GameActivity.java

public class GameActivity extends AppCompatActivity {
    private GuessWord guessWord;
    public int wrongGuesses = 0;
    public String wordToGuess;

    private void openGuessDialog() {
        guessWord = GuessWord.newInstance();
        guessWord.show(getSupportFragmentManager(), GuessWord.TAG);
    }
    public void runGuessWord() {
        GuessWord guessedWordInput = new GuessWord();
        guessedWordInput.setGuessedWord("");
        guessedWord = guessedWordInput.getGuessedWord();

        if (!guessedWord.equals("")) {
            if (guessedWord.equals(wordToGuess)) {
                gameWin();
            } else {
                wrongGuesses += 1;
                setImage();
                if (wrongGuesses == 7) {
                    gameOver();
                }
            }
        }
    }
}

GuessWord.java:

public class GuessWord extends DialogFragment {
    private GameActivity gameActivity;
    public String guessedWord;
    public EditText guessEntered;
    public static final String TAG = "guess";

    public static GuessWord newInstance() {
        Bundle args = new Bundle();
        GuessWord fragment = new GuessWord();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dialog_guess, container);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setCancelable(false);
        closeGuess();
        confirmGuess();
    }

    private void closeGuess() {
        Button closeGuess = getView().findViewById(R.id.btnCloseGuess);
        closeGuess.setOnClickListener(v -> {
            dismiss();
        });
    }

    private void confirmGuess() {
        Button confirmGuess = getView().findViewById(R.id.btnConfirmGuess);
        EditText guessEntered = getView().findViewById(R.id.etGuess);
        confirmGuess.setOnClickListener(v -> {
            guessedWord = guessEntered.getText().toString().toUpperCase();
            if (guessedWord.equals("")) {
                Toast.makeText(getActivity(), "Please enter a guess!", Toast.LENGTH_SHORT).show();
            } else {
                dismiss();
                System.out.println("Text Input = " + guessedWord);
                gameActivity.runGuessWord();
            }
        });
    }

    public String getGuessedWord() {
        return guessedWord;
    }
    public void setGuessedWord(String guessedWord) {
        this.guessedWord = guessedWord;
    }
}


Solution

  • You not assign value for gameActivity so it always null. Add it in your DialogFragment

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            gameActivity = ((GameActivity) getActivity());
        }