Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Hangman game HELP!

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hangman game HELP!

    I've been trying to make a hangman game for a high school project but lack of experience and skills prevents me from getting it to work This is probably easy for you all but i really need this help bec i dont know whats wrong with my code.

    Menu:
    package hangnerd;
    import javax.swing.*;
     
    /**
     *
     * @author Jonathan
     */
    public class GameMenu {
    HangGame hg = new HangGame();
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            GameMenu gameMenu = new GameMenu();
     
        }
     
        public GameMenu()
        {
            char opt =' ';
            do
            {
                menu();
                String choice = JOptionPane.showInputDialog("Enter an option");
                opt = choice.toUpperCase().charAt(0);
                options(opt);
     
            }
            while (opt != 'Q');
     
        }
       final void menu()
        {
            System.out.println("A. Play Game");
            System.out.println("Q. Quit");
     
        }
     void options(char opt)
        {
        switch(opt)
        {
            case 'A':hg.PlayGame();
            break;
     
        }
    }
    }

    Second class
    package hangnerd;
    import java.util.*;
    import java.io.*;
    /**
    *
    * @author Jonathan
    */
    public class WrdArray {
    WrdObj[] wordArr = new WrdObj[200];
    int count = 0;
    public void FileToArray() throws IOException
    {
    try{
    BufferedReader fr = new BufferedReader(new FileReader("Words.txt"));
    String line = fr.readLine();
    while(line != null)
    {
     
    StringTokenizer stk = new StringTokenizer(line,"#");
    String word = stk.nextToken();
    String hint = stk.nextToken();
    wordArr[count] = new WrdObj(word,hint);
    count++;
    line = fr.readLine();
    }
     
    } catch (FileNotFoundException fnfe)
    {
        System.out.println("File not found");
    }
    }
    public String RandomWrdGen()
    {
    int pos = (int) (Math.random()*count);
    String gameWrd = wordArr[pos].toString();
    return gameWrd;
    }
    public void Display()
    {
    System.out.println("URarray=" + Arrays.toString(wordArr));
    }
     
     
    public String toString()
    {
    String temp = "";
    for (int i = 0; i < count; i++)
    {
    temp += wordArr[i]+"\n";
    }
    return temp;
    }
    }

    thrid class
    package hangnerd;
     
    /**
     *
     * @author Jonathan
     */
    public class WrdObj {
    private String word;
    private String hint;
     
    public WrdObj()
    {
     
    }
     
    public WrdObj (String w,String h)
        {
        word = w;
        hint = h;
    }
     
    public String getWord()
        {
        return word;
    }
    public String getHint()
        {
        return hint;
    }
     
    public void setWord(String w)
        {
        word = w;
    }
     
    public void setHint(String h)
        {
        hint = h;
    }
     
    public String toString()
        {
        String temp;
        temp = word + " " + hint;
        return temp;
    }
    }

    fourth class
    package hangnerd;
    import javax.swing.*;
    /**
     *
     * @author Jonathan
     */
    public class HangGame {
    WrdArray wrd = new WrdArray();
            public void PlayGame()
        {
                String gWrd = wrd.RandomWrdGen();
            char newwrd[] = new char [100];
            for (int i = 0 ; i < gWrd.length () ; i++)
            {
                newwrd [i] = '-';
            }
            System.out.println (newwrd);
            int error = 0;
            int correct = 0;
            while (error != 5 && correct != gWrd.length ())
            {
                boolean flag = false;
                char guess = JOptionPane.showInputDialog ("Guess a letter").charAt (0);
                for (int o = 0 ; o < gWrd.length () ; o++)
                {
                    if (guess == gWrd.charAt (o))
                    {
                        newwrd [o] = guess;
                        correct++;
                        flag = true;
                    }
                }
                if (flag == false)
                {
                    error++;
                }
                System.out.println (newwrd);
            }
            if (correct == gWrd.length ())
            {
                JOptionPane.showMessageDialog (null, "You win!");
            }
            else
            {
                JOptionPane.showMessageDialog (null, "You lose!");
            }
        }
     
     
        }
    It will compile and I will get the chance to type in an option for my menu but after I input the letter A I get this message. When I input Q that part works.
    These are the errors:
    Quote Originally Posted by output
    run:
    A. Play Game
    Q. Quit
    Exception in thread "main" java.lang.NullPointerException
    at hangnerd.WrdArray.RandomWrdGen(WrdArray.java:41)
    at hangnerd.HangGame.PlayGame(HangGame.java:16)
    at hangnerd.GameMenu.options(GameMenu.java:47)
    at hangnerd.GameMenu.<init>(GameMenu.java:31)
    at hangnerd.GameMenu.main(GameMenu.java:19)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 4 seconds)
    Last edited by KingFisher; September 8th, 2010 at 04:04 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,038
    Thanks
    52
    Thanked 2,280 Times in 2,251 Posts

    Default Re: Hangman game HELP!

    i dont know whats wrong with my code.
    does it compile without errors?
    If not, please copy and paste the full text of the error messages.

    If it compiles, what does it do that is "wrong"? Can you show what it does do and explain what is wrong with that and show what you want it to do.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman game HELP!

    Quote Originally Posted by Norm View Post
    does it compile without errors?
    If not, please copy and paste the full text of the error messages.

    If it compiles, what does it do that is "wrong"? Can you show what it does do and explain what is wrong with that and show what you want it to do.
    I added what you asked for. So yes the program compiles without errors, its only once ive run it that I get the problems

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,038
    Thanks
    52
    Thanked 2,280 Times in 2,251 Posts

    Default Re: Hangman game HELP!

    at hangnerd.WrdArray.RandomWrdGen(WrdArray.java:41)
    What variable at line 41 of the WrdArray class is null? When you find the variable backtrack in your code to see why that variable is null and change the code so it is not null.

    If you are not sure which, add print out the values of all the variables that are used on line 41 before they are used.
    Last edited by Norm; September 8th, 2010 at 10:58 AM.

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman game HELP!

    Quote Originally Posted by Norm View Post
    What variable at line 41 of the WrdArray class is null? When you find the variable backtrack in your code to see why that variable is null and change the code so it is not null.

    If you are not sure which, add print out the values of all the variables that are used on line 41 before they are used.
    I think the problem is that my array isn't being filled? Did I go wrong in my code bec i certianly cant find where

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,038
    Thanks
    52
    Thanked 2,280 Times in 2,251 Posts

    Default Re: Hangman game HELP!

    i certianly cant find where
    To see what is in the array use the Arrays.toString() method to display the contents of the array.
    Usage: System.out.println("URarray=" + Arrays.toString(URarray)); // show URarray's contents

    Add this line where you are filling the array and again after you think it is full to show where you are missing adding items to the array.

    Also print out the value of the index you are using to fill the array and when access an element of the array.

  7. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman game HELP!

    Quote Originally Posted by Norm View Post
    To see what is in the array use the Arrays.toString() method to display the contents of the array.
    Usage: System.out.println("URarray=" + Arrays.toString(URarray)); // show URarray's contents

    Add this line where you are filling the array and again after you think it is full to show where you are missing adding items to the array.

    Also print out the value of the index you are using to fill the array and when access an element of the array.
    The values are all null!. wtf did I do wrong with my bufferedreader. Please check my code and see if you can find problem

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,038
    Thanks
    52
    Thanked 2,280 Times in 2,251 Posts

    Default Re: Hangman game HELP!

    Where did you print the values?
    For example what prints out here for the values of line, wordArr, count, word and hint?
    wordArr[count] = new WrdObj(word,hint);

    Add a println() statement after the above to show the values of all the variables shown above.

  9. #9
    Junior Member
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman game HELP!

    You don't call
    wrd.FileToArray();
    so you don't initialize wrd.
    Try:
    	public void PlayGame() {
    		try {
    			wrd.FileToArray();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		String gWrd = wrd.RandomWrdGen();
    		char newwrd[] = new char[100];
    		for (int i = 0; i < gWrd.length(); i++) {
    			newwrd[i] = '-';
    		}
    		System.out.println(newwrd);
    		int error = 0;
    		int correct = 0;
    		while (error != 5 && correct != gWrd.length()) {
    			boolean flag = false;
    			char guess = JOptionPane.showInputDialog("Guess a letter")
    					.charAt(0);
    			for (int o = 0; o < gWrd.length(); o++) {
    				if (guess == gWrd.charAt(o)) {
    					newwrd[o] = guess;
    					correct++;
    					flag = true;
    				}
    			}
    			if (flag == false) {
    				error++;
    			}
    			System.out.println(newwrd);
    		}
    		if (correct == gWrd.length()) {
    			JOptionPane.showMessageDialog(null, "You win!");
    		} else {
    			JOptionPane.showMessageDialog(null, "You lose!");
    		}
    	}
    Last edited by csaryus; September 9th, 2010 at 02:14 AM.

  10. #10
    Junior Member
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman game HELP!

    how to insert formated code?
    I'm new.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,038
    Thanks
    52
    Thanked 2,280 Times in 2,251 Posts

    Default Re: Hangman game HELP!


  12. #12
    Junior Member
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman game HELP!

    Quote Originally Posted by csaryus View Post
    You don't call
    wrd.FileToArray();
    so you don't initialize wrd.
    Thank you so much man you fixed it.
    And thanks to Norm as well for helping.

Similar Threads

  1. Snake Game
    By Cuju in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 19th, 2011, 07:31 PM
  2. Hangman
    By Tycho91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2010, 06:04 AM
  3. Game 3x3
    By Koren3 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 07:43 PM
  4. Java hangman game help...
    By AnotherNoob in forum AWT / Java Swing
    Replies: 16
    Last Post: December 4th, 2009, 10:17 PM
  5. Job offers to program Hobo Wars
    By MooncakeZ in forum Paid Java Projects
    Replies: 7
    Last Post: September 17th, 2009, 09:41 PM