Results 1 to 8 of 8
Thread: java.lang.NullPointerException
- 05-16-2011, 11:54 AM #1
Member
- Join Date
- May 2011
- Posts
- 17
- Rep Power
- 0
java.lang.NullPointerException
Hi,
Im new to java and im just messing around with a few things. Ive put thew following code togther but i get a java.lang.NullPointerException.
Any help would be very much appriciated.Java Code:package questionaireadvanced; import java.util.Scanner; public class QuestionaireAdvanced { public static void main(String[] args) { Scanner myScanner = new Scanner(System.in); String[] questions = new String[2]; questions[0] = "Whats is my name"; questions[1] = "What is my age"; char[] answer = new char[2]; for (int i = 0; i < questions.length; i++) { System.out.println(questions[i]); answer[i] = myScanner.findInLine(".").charAt(0); switch (answer[i]) { case 'a': System.out.println("Wrong!"); break; case 'b': System.out.println("Well done!"); break; case 'c': System.out.println("Wrong!"); break; case 'd': System.out.println("Wrong!"); break; default: System.out.println("You did not enter a, b, c, or d"); break; } } System.out.println("The program is now exiting"); } }
- 05-16-2011, 12:13 PM #2
Could we get a stacktrace of the exception (i.e. what is output to the console)? It makes it much easier pinpointing the error.
- 05-16-2011, 12:19 PM #3
Member
- Join Date
- May 2011
- Posts
- 17
- Rep Power
- 0
Hi Toll,
Here is the output
ThanksJava Code:run: Whats is my name b Well done! What is my age Exception in thread "main" java.lang.NullPointerException at questionaireadvanced.QuestionaireAdvanced.main(QuestionaireAdvanced.java:18) Java Result: 1 BUILD SUCCESSFUL (total time: 2 seconds)
- 05-16-2011, 12:31 PM #4
It seems to indicate that the problem is in "answer[i] = myScanner.findInLine(".").charAt(0);". After reading the docs for Scanner (I must admit, I've never really used Scanner), I'm starting to believe that the problem stems from it not finding the pattern in the line. Does the program stop before you enter the second answer, or after?
- 05-16-2011, 12:55 PM #5
Member
- Join Date
- May 2011
- Posts
- 17
- Rep Power
- 0
The second question is printed on the screen but ends when it want to take input. Here is another run.
Java Code:run: Whats is my name a Wrong! Exception in thread "main" java.lang.NullPointerException What is my age at questionaireadvanced.QuestionaireAdvanced.main(QuestionaireAdvanced.java:18) Java Result: 1 BUILD SUCCESSFUL (total time: 2 seconds)
- 05-16-2011, 01:09 PM #6
Then I'm rather certain that the findInLine() method doesn't actually advance lines, but just looks in the last entered line. So it's trying to find the next character in the line with your first answer, and since it can't find any more characters, it returns null according to its specifications. Try finding another method to use; have a look at Scanner (Java Platform SE 6) to see if you can find one that suits you.
- 05-16-2011, 02:23 PM #7
Member
- Join Date
- May 2011
- Posts
- 17
- Rep Power
- 0
After thinking about what you said i ran the program again with entering two charcters. The program ran without any errors.
I had a look through the scanner documentation you provided and i carnt see a methord that suits my needs in there.Java Code:run: Whats is my name ab Wrong! What is my age Well done! The program is now exiting BUILD SUCCESSFUL (total time: 2 seconds)
COuld you recommend somthing else?
- 05-16-2011, 03:01 PM #8
Similar Threads
-
java.lang.NullPointerException
By nitinverma in forum CLDC and MIDPReplies: 4Last Post: 06-15-2010, 03:56 PM -
java.lang.nullPointerException
By KSUliz in forum New To JavaReplies: 10Last Post: 04-11-2010, 07:15 PM -
java.lang.NullPointerException
By stevemcc in forum AWT / SwingReplies: 2Last Post: 02-08-2008, 09:01 AM -
java.lang.NullPointerException
By ravian in forum New To JavaReplies: 1Last Post: 01-13-2008, 07:39 PM -
java.lang.NullPointerException
By Felissa in forum Advanced JavaReplies: 1Last Post: 07-05-2007, 06:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks