Results 1 to 9 of 9
Thread: NullPointerException
- 02-21-2010, 02:16 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 20
- Rep Power
- 0
NullPointerException
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.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()); } }
- 02-21-2010, 02:19 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,423
- Blog Entries
- 7
- Rep Power
- 17
- 02-21-2010, 02:23 PM #3
Member
- Join Date
- Feb 2010
- Posts
- 20
- Rep Power
- 0
Yeah, I've got a second NullPointerException:
Has it got something to do with the declaration of the guessword?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; }
-
- 02-21-2010, 02:37 PM #5
Member
- Join Date
- Feb 2010
- Posts
- 20
- Rep Power
- 0
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.
- 02-21-2010, 02:58 PM #6
Member
- Join Date
- Feb 2010
- Posts
- 20
- Rep Power
- 0
Ok, never mind. I think I fixed it.
- 02-21-2010, 02:59 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,423
- Blog Entries
- 7
- Rep Power
- 17
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:
... 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.Java Code:private StringBuffer guessword= new StringBuffer();
kind regards,
Jos
-
Yes, but you had information that we didn't have, namely the line numbers that the NPE fell on.
Then you are correct, guessword is null when you are trying to dereference it. Solution: construct it before using it.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.
- 02-21-2010, 03:05 PM #9
Member
- Join Date
- Feb 2010
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
NullPointerException
By Juuno in forum New To JavaReplies: 1Last Post: 02-11-2010, 05:43 PM -
NullPointerException: I can't get rid of it.
By mcashe in forum AWT / SwingReplies: 2Last Post: 08-17-2009, 09:16 PM -
NullPointerException
By adeeb in forum AWT / SwingReplies: 3Last Post: 06-11-2008, 08:42 AM -
NullPointerException
By mensa in forum Java 2DReplies: 5Last Post: 05-03-2008, 11:19 PM -
NullPointerException
By ravian in forum New To JavaReplies: 2Last Post: 12-07-2007, 04:20 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks