Results 1 to 9 of 9
  1. #1
    GPB
    GPB is offline Member
    Join Date
    Feb 2010
    Posts
    20
    Rep Power
    0

    Default NullPointerException

    Java Code:
    import java.util.Scanner;
    
    public class Main {
    
        public static void main(String[] args) {
            [COLOR="Red"]Hangman method = new Hangman("Boat");[/COLOR]
            Scanner scan = new Scanner ( System.in );
            System.out.printf("===== H A N G M A N =====");
            while ( method.nextLetter() ) {
                System.out.printf("%n%s",method.status());
                System.out.printf("%nInput letter: ");
                String input = scan.nextLine();
                char ch = input.charAt(0);
                method.try(ch);
            }
            System.out.printf("%n%s",method.status());
        }
    }
    Hi, I'm new to Java. The following code gives a NullPointerException at the line marked red. I've searched through a couple of threads, but I'm still not really getting what the error means.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by GPB View Post
    Java Code:
    import java.util.Scanner;
    
    public class Main {
    
        public static void main(String[] args) {
            [COLOR="Red"]Hangman method = new Hangman("Boat");[/COLOR]
            Scanner scan = new Scanner ( System.in );
            System.out.printf("===== H A N G M A N =====");
            while ( method.nextLetter() ) {
                System.out.printf("%n%s",method.status());
                System.out.printf("%nInput letter: ");
                String input = scan.nextLine();
                char ch = input.charAt(0);
                method.try(ch);
            }
            System.out.printf("%n%s",method.status());
        }
    }
    Hi, I'm new to Java. The following code gives a NullPointerException at the line marked red. I've searched through a couple of threads, but I'm still not really getting what the error means.
    A constructor (of the Hangman class) can't return a null pointer; something else must be wrong. Show your code please.

    kind regards,

    Jos

  3. #3
    GPB
    GPB is offline Member
    Join Date
    Feb 2010
    Posts
    20
    Rep Power
    0

    Default

    Yeah, I've got a second NullPointerException:
    Java Code:
    package javaapplication2;
    
    public class Hangman {
        
        private int tries;
        private String targetword;
        private StringBuffer guessword;
        
        public Hangman(String s) {
            doelwoord = s;
            int length = targetword.length();
            for ( int x = 0 ; x < length ; x++ ) {
                [COLOR="Red"]guessword.setCharAt(x,'.');[/COLOR]
            }
            tries = 10;
        }
    Has it got something to do with the declaration of the guessword?

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

    Default

    Quote Originally Posted by GPB View Post
    Yeah, I've got a second NullPointerException:
    Has it got something to do with the declaration of the guessword?
    You tell us. You have the error message with its line numbers.

  5. #5
    GPB
    GPB is offline Member
    Join Date
    Feb 2010
    Posts
    20
    Rep Power
    0

    Default

    Ehm.. If I knew what was wrong then I wouldn't need to ask here right? I have too errors,
    The error message is:
    Exception in thread "main" java.lang.NullPointerException
    at javaapplication2.Hangman.<init>(Hangman.java:22)
    at javaapplication2.Main.main(Main.java:21)
    Java Result: 1
    which are the red lines in the above code.
    Last edited by GPB; 02-21-2010 at 02:39 PM.

  6. #6
    GPB
    GPB is offline Member
    Join Date
    Feb 2010
    Posts
    20
    Rep Power
    0

    Default

    Ok, never mind. I think I fixed it.

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

    Default

    Quote Originally Posted by GPB View Post
    Yeah, I've got a second NullPointerException:
    Java Code:
    package javaapplication2;
    
    public class Hangman {
        
        private int tries;
        private String targetword;
        private StringBuffer guessword;
        
        public Hangman(String s) {
            doelwoord = s;
            int length = targetword.length();
            for ( int x = 0 ; x < length ; x++ ) {
                [COLOR="Red"]guessword.setCharAt(x,'.');[/COLOR]
            }
            tries = 10;
        }
    Has it got something to do with the declaration of the guessword?
    You don't have a StringBuffer guessword object yet, just a reference to such an object but the reference is still null. Change that declaration to:

    Java Code:
    private StringBuffer guessword= new StringBuffer();
    ... that way you declare your member variable guessword and initialize it at the same time. btw, learn to read those exception stacktraces: the first line mentions what exception was thrown, the next line mentions where it was thrown, the following line mentions the caller of that method and so on.

    kind regards,

    Jos

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

    Default

    Quote Originally Posted by GPB View Post
    Ehm.. If I knew what was wrong then I wouldn't need to ask here right?
    Yes, but you had information that we didn't have, namely the line numbers that the NPE fell on.

    I have too errors,
    The error message is:
    Exception in thread "main" java.lang.NullPointerException
    at javaapplication2.Hangman.<init>(Hangman.java:22)
    at javaapplication2.Main.main(Main.java:21)
    Java Result: 1
    which are the red lines in the above code.
    Then you are correct, guessword is null when you are trying to dereference it. Solution: construct it before using it.

  9. #9
    GPB
    GPB is offline Member
    Join Date
    Feb 2010
    Posts
    20
    Rep Power
    0

    Default

    Yeah I figured it out as well, haha! It's good to know how to read the error though, thanks ;)

Similar Threads

  1. NullPointerException
    By Juuno in forum New To Java
    Replies: 1
    Last Post: 02-11-2010, 05:43 PM
  2. NullPointerException: I can't get rid of it.
    By mcashe in forum AWT / Swing
    Replies: 2
    Last Post: 08-17-2009, 09:16 PM
  3. NullPointerException
    By adeeb in forum AWT / Swing
    Replies: 3
    Last Post: 06-11-2008, 08:42 AM
  4. NullPointerException
    By mensa in forum Java 2D
    Replies: 5
    Last Post: 05-03-2008, 11:19 PM
  5. NullPointerException
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 04:20 PM

Posting Permissions

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