Results 1 to 20 of 23
- 07-20-2011, 09:57 PM #1
nOOb :)
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
Java.Lang.NullPointerException, but I am not sure why???
Hello all, I have just begun to dive into the Java world. I have been following through some books and have gotten stuck on a spot in a DO While Loop. Why am I getting the NullPointException error here?
The error is happening here:Java Code:import java.util.Scanner; import static java.lang.System.out; class ReadAndWrite{ public static void main(String args[]) { Scanner myScanner = new Scanner(System.in); int whichRoom, numGuests; int guestsIn[]; guestsIn = new int[10]; for(int roomNum = 0; roomNum < 10; roomNum++){ guestsIn[roomNum] = 0; } do{ System.out.print("Room number: "); whichRoom = myScanner.nextInt(); System.out.print("How Many Guests? "); numGuests = myScanner.nextInt(); guestsIn[whichRoom] = numGuests; System.out.println(); System.out.print("Do another? "); }while (myScanner.findInLine(".").charAt(0) == 'Y'); System.out.println(); System.out.println("Room\tGuests"); for(int roomNum = 0; roomNum < 10; roomNum++){ System.out.print(roomNum); System.out.print("\t"); System.out.println(guestsIn[roomNum]); } } }
Thanks for any suggestions!Java Code:System.out.println(); System.out.print("Do another? "); }while (myScanner.findInLine(".").charAt(0) == 'Y');Last edited by buildakicker; 07-20-2011 at 09:59 PM. Reason: added more code...
- 07-20-2011, 10:53 PM #2
Please copy and paste here the full text of the error message.Why am I getting the NullPointException error here
(myScanner.findInLine(".").charAt(0)
What values does the findInLine method return? Can it be null?
- 07-20-2011, 11:16 PM #3
nOOb :)
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
Error is:
I don't have that set to accept null. That is where I am confused. I cannot even enter Y or N though... that is what I dont' get.Java Code:Do another? Exception in thread "main" java.lang.NullPointerException at ReadAndWrite.main(ReadAndWrite.java:25)
- 07-20-2011, 11:23 PM #4
What values does the findInLine() method return?
Print out its value before you use it.
copy and paste here the full text of the console for when you test your program.
- 07-20-2011, 11:29 PM #5
nOOb :)
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
this is the whole console:
thanks :)Java Code:Room number: 5 How Many Guests? 5 Do another? Exception in thread "main" java.lang.NullPointerException at ReadAndWrite.main(ReadAndWrite.java:25)
- 07-20-2011, 11:35 PM #6
Double check the line where the error is occurring.
The output from this line has not printed yet: System.out.print("Do another? ")
so the error must occur before that.
To be sure, print out the value returned by the call to the findInLine() method.
- 07-20-2011, 11:47 PM #7
nOOb :)
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
I'm sorry I guess I don't follow you. I get it to print out System.out.print("Do Another? "); but then get the exception error. Doesn't it check after I have entered data in the While loop?
- 07-20-2011, 11:50 PM #8
Sorry, I missed it on the same line as the error message.
Print out the value returned by the call to the findInLine() method before executing the while() statement.
- 07-21-2011, 12:35 AM #9
nOOb :)
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
I am confused in my noob java brain. How do I pass it NULL as being ok then? It's looking for something there.
- 07-21-2011, 12:38 AM #10
Please explain what the "it" is in these two sentences.How do I pass it NULL as being ok then? It's looking for something there.
What prints out when you do this:
Print out the value returned by the call to the findInLine() method before executing the while() statement.
- 07-21-2011, 12:40 AM #11
nOOb :)
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
Ok, so I added this:
and the Console returned:Java Code:do{ System.out.print("Room number: "); whichRoom = myScanner.nextInt(); System.out.print("How Many Guests? "); numGuests = myScanner.nextInt(); guestsIn[whichRoom] = numGuests; System.out.println(); [B]System.out.println(myScanner.findInLine("."));[/B] System.out.print("Do another? "); }while (myScanner.findInLine(".").charAt(0) == 'Y');
Java Code:Room number: 5 How Many Guests? 5 null Do another? Exception in thread "main" java.lang.NullPointerException at ReadAndWrite.main(ReadAndWrite.java:28)
- 07-21-2011, 12:42 AM #12
While loop with case - getting error
Read my reply #3 in the above thread.
- 07-21-2011, 12:48 AM #13
Why are you using findInLine anyway. I'd just read the line and use equalsIgnoreCase instead.
- 07-21-2011, 12:52 AM #14
Copy and pasted it?
- 07-21-2011, 12:53 AM #15
nOOb :)
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
I switched it from findInLine to next and it seems to work fine. Is this bad programming? lol
I don't understand the:While loop with case - getting error
Read my reply #3 in the above thread.How do I throw it away?Quick and dirty solution call nextLine and throw it away immediately after the call to nextInt.
- 07-21-2011, 12:54 AM #16
nOOb :)
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
- 07-21-2011, 12:55 AM #17
Throw it away is another way of saying, ignore what was returned.
Here's some thoughts I put together awhile back:
Scanner methods can be tricky. The Scanner buffers input and can block waiting for input.
For example if you enter: A word to the wise <PRESS ENTER>
and use next() only "A" is read, and "word to the wise" is left in the buffer.
Your next attempt to get something from Scanner will be to get "word".
nextInt() will fail here.
To clear the buffer, use the nextLine() method.
To test if the next token is an int, use the hasNextInt() method.
- 07-21-2011, 01:01 AM #18
nOOb :)
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
So like this:
...and that will "clear" the buffer also?Java Code:System.out.print("Do another? "); myScanner.nextLine(); }while (myScanner.findInLine(".").charAt(0)== 'Y');Last edited by buildakicker; 07-21-2011 at 01:02 AM. Reason: [code]
- 07-21-2011, 01:03 AM #19
- 07-21-2011, 01:04 AM #20
Similar Threads
-
java.lang.NullPointerException and i don't know why
By Qubes in forum New To JavaReplies: 15Last Post: 01-13-2011, 06:28 PM -
java.lang.NullPointerException
By peterhabe in forum New To JavaReplies: 5Last Post: 08-27-2010, 08:43 PM -
java.lang.NullPointerException
By Pombi in forum New To JavaReplies: 6Last Post: 05-15-2010, 03:12 PM -
java.lang.NullPointerException
By vasavi.singh in forum New To JavaReplies: 1Last Post: 02-27-2009, 12:36 PM -
java.lang.NullPointerException
By stevemcc in forum AWT / SwingReplies: 2Last Post: 02-08-2008, 09:01 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks