Results 1 to 12 of 12
  1. #1
    vouslavous is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default help with part of my hangman applet

    alright so im in the process of building a hangman game in which someone enters a word (the word someone will have to figure out). and the other player guesses the letters. first off i have an attachment including the picture of the layout


    those lines with blank spaces are where correct letters will be displayed, that blackbox will eventually be a picture of a person hanging and progress as you get more incorrect letters (you know what i mean if you ever played hang man).

    the problem im having is with the guess button, it gives me an error when i run the program, specifically null pointer error. ill post the code for it all now

    Java Code:
    public class hangman extends javax.swing.JFrame {
    // varibles
        String correctWord;
        String guess;
        String correctSpace1;
        String correctSpace2;
        String correctSpace3;
        String correctSpace4;
        String correctSpace5;
     // buttonCheks
    
        /** Creates new form hangman */
        public hangman() {
            initComponents();
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
    
    
        private void guessButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    guess = letterEnter.getText();
    if (guess.length()>1) //checks to see if the guess is only 1 char long
    {
    letterEnter.setText("");
    }
    else
    {
    for (int i = 0; i<=correctWord.length(); i++)  // compares guessletter with every letter in correct word
    {
        String tester = correctWord.substring(i);
        if (guess.equals(tester))
        {
            System.out.println("aaa");
        }
        else
        wrongLetterField.setText(wrongLetterField.getText()+letterEnter.getText());  // if wrong adds to the incorect letter field
    
    }
    }
    if you have any quesstions just tell me or if you have suggestions feel free to post
    Attached Thumbnails Attached Thumbnails help with part of my hangman applet-hangman-app-picture.jpg  

  2. #2
    vouslavous is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    i cut down about 200 lines of generated code so its easier to read, if you want the entire code just tell me

  3. #3
    vouslavous is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    that system.out.println("aa");
    is just to test that it knows if its correct, ill edit that to do the correct thing later

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    which line throws the NPE?

  5. #5
    vouslavous is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    i belive its in the forloop or has to do with it

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    No need to guess as the error message will tell you exactly what line throws the NPE. Your job is to pass this information on to us.

  7. #7
    vouslavous is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    i just told you its at the for loop line...

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    and I told you that the compiler will tell you the exact line. We're not looking for a for-loop block, we're looking for an exact line. Up to you.

  9. #9
    vouslavous is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    its not caught at compile time, its caught when it runs, its the forloop line the actual construction of the for loop

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    ah, OK. So there is only one object on that line: correctWord, and it is null. Do you ever set correctWord to hold the "correct" word?

  11. #11
    vouslavous is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    k, correct word isnt null anymore but when i run the loop it still registers the letter as incorrect even thoush it should be, and it prints it out multiple times in the incorrect letter field

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You've got a bug, and so it's time to do some debugging. You should sprinkle your code with println statements to determine the state of your program's variables at various points in the program, either that or run it through a debugger.

Similar Threads

  1. Replies: 10
    Last Post: 12-30-2008, 04:46 PM
  2. Need help with Hangman!!!
    By chinasome in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 05:42 AM
  3. Hangman Help!!!
    By chinasome in forum New To Java
    Replies: 5
    Last Post: 11-08-2008, 03:30 AM
  4. Hangman Game
    By L23 in forum New To Java
    Replies: 8
    Last Post: 07-03-2008, 02:56 PM
  5. Create the game Hangman
    By barney in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:16 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
  •