Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default I don't know how to debug this error: Null Pointer Exception

    Although i found it in hangman.java line 41, but nothing i can see.
    Exception in thread "main" java.lang.NullPointerException
    at Hangman.play(Hangman.java:41)
    at HangmanGame.startGame(HangmanGame.java:22)
    at HangmanGame.main(HangmanGame.java:43)
    Java Result: 1

    hangman.java
    Java Code:
    public class Hangman {
    
        private String secretWord;
        private String printedSecretWord;
        private HangmanUI hangmanUI;
        private char[] guessedCharacters;
        private int numberOfRounds;
        private int numberOfMissedCharacters;
    
    
        public Hangman(String inputSecretWord, HangmanUI inputHangmanUI) {
            secretWord = inputSecretWord;  
            hangmanUI = inputHangmanUI;  
            
            for (int i = 0; i < secretWord.length(); i++)
            {printedSecretWord = printedSecretWord + "_";}
            
            numberOfRounds = 0;
            numberOfMissedCharacters = 0;
            guessedCharacters = new char[6];
        }
    
        private boolean hitSecretWord(char inputCharacter) {
            boolean result = false;
            if(secretWord.indexOf(inputCharacter)!=-1)  //return value '-1' means input character is not match in secretWord
                {
                    result =true;     //if return the vaule is not -1, that's mean input character is in secret word
                }       
            return result;  //by default the result is false, if guess character is not contain in the secret word, 
        }
    
        private void modifyPrintedWord(char guessedCharacter){
            int i;
            StringBuilder strBuilder = new StringBuilder(printedSecretWord);
            for (i = 0; i < secretWord.length(); i++)
            {if (secretWord.charAt(i) == guessedCharacter){ 
            strBuilder.setCharAt(i*2, guessedCharacter);} //i*2, due to there has a space between secret Word in output format 
            }
            printedSecretWord = strBuilder.toString();        
        }
        private boolean hasWon() {
            boolean result = true;
            if(printedSecretWord.indexOf('_')!=-1)  // checking the printed secret Word Without "_", that's mean all the character was correctly guess and replace by the corrected answer
                {
                    result =false;     // if there is contain "_",that's not won ,so returen false
                }       
            return result; //otherwise, retuned true
            }
    
        private boolean wasPreviouslyTyped(char inputCharacter) {
            boolean result = false;     
            int i;
            for (i = 0; i < numberOfRounds; i++)  
                    {if (guessedCharacters[i] == inputCharacter) //guessedCharacters is a char array stored all the input 
                        {result = true;  // if check the inpput character was previously typed
                         return result;  //then return the result is true
                        }
                    }
            return result;
        }
       
        public void drawOutput() {
        hangmanUI.printSecretWord(printedSecretWord);     
        hangmanUI.printTypedCharacters(guessedCharacters, numberOfRounds);
        hangmanUI.printCurrentRound(numberOfRounds);
        hangmanUI.drawHangman(numberOfMissedCharacters);
        }
     
        public int play(char guessedCharacter) {
            /* We have implemented this method for you.
             * You do not need to implement this method,
             * but you should try to understand the logic in this method.
             */
            guessedCharacters[numberOfRounds] = guessedCharacter;
            int returnState;
            if (wasPreviouslyTyped(guessedCharacter)) {
                returnState = 2;
            } else {
                if (hitSecretWord(guessedCharacters[numberOfRounds]) == true) {                
                    modifyPrintedWord(guessedCharacter);
                }else{
                    numberOfMissedCharacters++;
                }
                numberOfRounds++;
                if (numberOfMissedCharacters >= 6) {
                    returnState = 0;
                } else if (hasWon()) {
                    returnState = 1;
                } else {
                    returnState = 3;
                }
            }
            return returnState;
        }
    }
    hangmangame.java
    Java Code:
    public class HangmanGame {
    
        private HangmanUI hangmanUI;
        private String secretWord;
        private Hangman hangman;
        private boolean gameFinished;
    
        public HangmanGame() {
            hangmanUI = null;
            secretWord = null;
            hangman = null;
            gameFinished = false;
        }
    
        public void startGame() {
            hangmanUI = new HangmanUI();
            secretWord = hangmanUI.getSecretWordFromTerminal();
            hangman = new Hangman(secretWord, hangmanUI);
            while (gameFinished == false) {
                hangman.drawOutput();
                char inputCharacter = hangmanUI.getNextCharacter();
                int state = hangman.play(inputCharacter);
    
                if (state != 3) {
                    if (state == 0) {
                        hangman.drawOutput();
                        hangmanUI.printDeadMessage();
                        hangmanUI.playLosingSound();
                        gameFinished = true;
                    } else if (state == 1) {
                        hangman.drawOutput();
                        hangmanUI.printWinningMessage();
                        hangmanUI.playWinningSound();
                        gameFinished = true;
                    } else if (state == 2) {
                        hangmanUI.printRepeatedCharacterMessage();
                    }
                }
            }
        }
        public static void main(String[] args) {
            HangmanGame hangmanGame = new HangmanGame();
            hangmanGame.startGame();
        }
    }
    hangmanUI.java
    Java Code:
    import java.io.*;
    import java.util.*;
    import javax.sound.sampled.*;
    import javax.swing.*;
    
    public class HangmanUI {
    
       public void playWinningSound() {
            try {
                Clip clip = AudioSystem.getClip();
                clip.open(AudioSystem.getAudioInputStream(new File("win.wav")));
                clip.start();
                Thread.sleep(100);
                clip.drain();
                while(clip.isRunning()==true){
                Thread.sleep(100);
                }
                
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void playLosingSound() {
            try {
                Clip clip = AudioSystem.getClip();
                clip.open(AudioSystem.getAudioInputStream(new File("lose.wav")));
                clip.start();
                Thread.sleep(100);
                clip.drain();
                while(clip.isRunning()==true){
                Thread.sleep(100);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public String getSecretWordFromTerminal() {
            Scanner keyboard = new Scanner(System.in);
            String inputString = null;
        do {
    
                System.out.print("Enter a secret word:");
                inputString = keyboard.next();
            } while (checkSecretWord(inputString) == false);
            return inputString;
        }
    
        public boolean checkSecretWord(String secretWord) {
            boolean noerror = true;
            if (secretWord.length() > 20) {
                JOptionPane.showMessageDialog(null, "Secret word " + secretWord + " too long!");
                noerror = false;
            }
            if (secretWord.matches("([a-z]|[A-Z])+") == false) {
                JOptionPane.showMessageDialog(null, "Secret word " + secretWord + " has non-alphabetical characters!");
                noerror = false;
            }
            return noerror;
        }
        public boolean checkInputString(String inputString) {
            boolean noerror = true;
            if (inputString.length() != 1) {
                System.out.println("There is more than one character!");
                noerror = false;
            }
            if (inputString.matches("([a-z]|[A-Z])+") == false) {
                System.out.println("There are non-alphabetical characters!");
                noerror = false;
            }
            return noerror;
        }
        public char getNextCharacter() {
            String inputString = null;
            Scanner keyboard = new Scanner(System.in);
            do {
                System.out.print("Enter a character:");
                inputString = keyboard.next();
            } while (checkInputString(inputString) == false);
            return inputString.charAt(0);
        }
        public void printCurrentRound(int currentRound) {
            System.out.println("Current Round: " + currentRound);
        }
        public void printDeadMessage() {
            System.out.println("You are dead!");
        }
        public void printWinningMessage() {
            System.out.println("You win!");
        }
        public void printRepeatedCharacterMessage() {
            System.out.println("You have previously entered the character!");
        }
        public void printTypedCharacters(char[] typedCharacters, int round) {
            if (round == 0) {
                System.out.println("No characters have been typed");
            } else {
                System.out.print("Tried alphabets: ");
                for (int i = 0; i < round; i++) {
                    if (i != 0) {
                        System.out.print(",");
                    }
                    System.out.print(typedCharacters[i]);
                }
                System.out.print("\n");
            }
        }
        public void printSecretWord(String printedSecretWord) {
            System.out.println("String:");
            for (int stringIndex = 0; stringIndex < printedSecretWord.length(); stringIndex++) {            
                    System.out.print(printedSecretWord.charAt(stringIndex) + " ");            
            }
            System.out.print("\n");
        }
        public void drawHangman(int numberOfMissedCharacters) {
            System.out.println("_______________     ");
            System.out.println("|             |     ");
            if (numberOfMissedCharacters == 0) {
                System.out.println("|                   ");
                System.out.println("|                   ");
                System.out.println("|                   ");
            } else {
                System.out.println("|             o     ");
                System.out.println("|            o o    ");
                System.out.println("|             o     ");
            }
            if (numberOfMissedCharacters <= 1) {
                System.out.println("|                   ");
                System.out.println("|                   ");
                System.out.println("|                   ");
                System.out.println("|                   ");
                System.out.println("|                   ");
                System.out.println("|                   ");
            } else if (numberOfMissedCharacters == 2) {
                System.out.println("|             |     ");
                System.out.println("|             |     ");
                System.out.println("|             |     ");
                System.out.println("|                   ");
                System.out.println("|                   ");
                System.out.println("|                   ");
            } else if (numberOfMissedCharacters == 3) {
                System.out.println("|             |     ");
                System.out.println("|            /|     ");
                System.out.println("|           / |     ");
                System.out.println("|                   ");
                System.out.println("|                   ");
                System.out.println("|                   ");
            } else if (numberOfMissedCharacters == 4) {
                System.out.println("|             |     ");
                System.out.println("|            /|\\    ");
                System.out.println("|           / | \\   ");
                System.out.println("|                   ");
                System.out.println("|                   ");
                System.out.println("|                   ");
            } else if (numberOfMissedCharacters == 5) {
                System.out.println("|             |     ");
                System.out.println("|            /|\\    ");
                System.out.println("|           / | \\   ");
                System.out.println("|            /      ");
                System.out.println("|           /       ");
                System.out.println("|          /        ");
            } else if (numberOfMissedCharacters == 6) {
                System.out.println("|             |     ");
                System.out.println("|            /|\\    ");
                System.out.println("|           / | \\   ");
                System.out.println("|            / \\    ");
                System.out.println("|           /   \\   ");
                System.out.println("|          /     \\  ");
            }
        }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: I don't know how to debug this error: Null Pointer Exception

    Which line is line 41?
    It's not the line 41 in the code you posted.
    Indeed, since it is apparently in the play() method, that is not the code that produced this stack trace.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: I don't know how to debug this error: Null Pointer Exception

    i am using netbeans to complie the java,
    there has totally 3 java file. hangmangame , hangman, hangmanUI
    and i choose the hangmangame.java to run it, because only 1 main method in that file.
    and it shown the error message when it is loading the hasWon method
    Last edited by newlife20111007; 11-20-2012 at 10:47 AM.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,354
    Blog Entries
    7
    Rep Power
    20

    Default Re: I don't know how to debug this error: Null Pointer Exception

    Quote Originally Posted by newlife20111007 View Post
    i am using netbeans to complie the java,
    there has totally 3 java file. hangmangame , hangman, hangmanUI
    and i choose the hangmangame.java to run it, because only 1 main method in that file.
    and it shown the error message when it is loading the hasWon method
    If you want help then please be clear to us: the line #41 (see above) we're seeing is not line #41 in your code; you don't want us to guess do you?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: I don't know how to debug this error: Null Pointer Exception

    thank you very much for quick reply.
    actually i am using netbeans and i seen that, the line 41 is exactly #41 private boolean hasWon()
    and i try to capture the screen and help you all can help me.
    because i am very new for the java.
    {I don't know how to debug this error: Null Pointer Exception-hangman.jpgI don't know how to debug this error: Null Pointer Exception-hangmangame.jpgI don't know how to debug this error: Null Pointer Exception-hangmanui.jpg

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: I don't know how to debug this error: Null Pointer Exception

    That is not the code that is running then.
    That line cannot possibly throw a NullPointerException.

    Indeed, look at the little red 'x' by your Hangman.java.
    That implies there is an error in the file.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: I don't know how to debug this error: Null Pointer Exception

    thank you very much anyway,
    actually, i am very wonder that why it has null pointer exception will occur there....
    should i import the java from the beginning to prevent the problem?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: I don't know how to debug this error: Null Pointer Exception

    What has happened is you are running old code because you current code is not compiling.
    At least that's what those screenshots look like to me.

    So sort out your compilation problems first.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Null Pointer Exception
    By maestroX3 in forum New To Java
    Replies: 13
    Last Post: 05-22-2012, 09:04 PM
  2. how to solve debug this null pointer
    By aborgeld in forum Advanced Java
    Replies: 5
    Last Post: 05-13-2011, 11:48 AM
  3. Null Pointer Exception error
    By tfitz666 in forum New To Java
    Replies: 3
    Last Post: 03-28-2010, 07:20 PM
  4. Null pointer exception?
    By coffee in forum New To Java
    Replies: 4
    Last Post: 08-03-2009, 03:22 AM
  5. Null pointer exception error
    By brownie_jedi in forum New To Java
    Replies: 3
    Last Post: 03-15-2008, 06:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •