Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    buildakicker is offline nOOb :)
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Question 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?

    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]);
    			}
    	}
    }
    The error is happening here:

    Java Code:
    System.out.println();
    				System.out.print("Do another? ");
    			}while (myScanner.findInLine(".").charAt(0) == 'Y');
    Thanks for any suggestions!
    Last edited by buildakicker; 07-20-2011 at 09:59 PM. Reason: added more code...

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    Why am I getting the NullPointException error here
    Please copy and paste here the full text of the error message.

    (myScanner.findInLine(".").charAt(0)

    What values does the findInLine method return? Can it be null?

  3. #3
    buildakicker is offline nOOb :)
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    Error is:
    Java Code:
    Do another? Exception in thread "main" java.lang.NullPointerException
    	at ReadAndWrite.main(ReadAndWrite.java:25)
    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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    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.

  5. #5
    buildakicker is offline nOOb :)
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    this is the whole console:

    Java Code:
    Room number: 5
    How Many Guests? 5
    
    Do another? Exception in thread "main" java.lang.NullPointerException
    	at ReadAndWrite.main(ReadAndWrite.java:25)
    thanks :)

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    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.

  7. #7
    buildakicker is offline nOOb :)
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    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?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    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.

  9. #9
    buildakicker is offline nOOb :)
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    I am confused in my noob java brain. How do I pass it NULL as being ok then? It's looking for something there.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    How do I pass it NULL as being ok then? It's looking for something there.
    Please explain what the "it" is in these two sentences.

    What prints out when you do this:
    Print out the value returned by the call to the findInLine() method before executing the while() statement.

  11. #11
    buildakicker is offline nOOb :)
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    Ok, so I added this:

    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');
    and the Console returned:
    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)

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    While loop with case - getting error

    Read my reply #3 in the above thread.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Why are you using findInLine anyway. I'd just read the line and use equalsIgnoreCase instead.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    Copy and pasted it?

  15. #15
    buildakicker is offline nOOb :)
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    I switched it from findInLine to next and it seems to work fine. Is this bad programming? lol

    While loop with case - getting error

    Read my reply #3 in the above thread.
    I don't understand the:
    Quick and dirty solution call nextLine and throw it away immediately after the call to nextInt.
    How do I throw it away?

  16. #16
    buildakicker is offline nOOb :)
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Copy and pasted it?
    I've been typing it all in there thanks. :)

    I have that Java for Dummies book and have been going through it.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    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.

  18. #18
    buildakicker is offline nOOb :)
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    So like this:

    Java Code:
    System.out.print("Do another? ");
    				myScanner.nextLine();
    			}while (myScanner.findInLine(".").charAt(0)== 'Y');
    ...and that will "clear" the buffer also?
    Last edited by buildakicker; 07-21-2011 at 01:02 AM. Reason: [code]

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by buildakicker View Post
    How do I throw it away?
    Simply call the method but do not assign the return value to a variable.

  20. #20
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by buildakicker View Post
    So like this:
    Yep but I would move that line upto where you call nextInt to make it clear as to what the code is doing.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 15
    Last Post: 01-13-2011, 06:28 PM
  2. java.lang.NullPointerException
    By peterhabe in forum New To Java
    Replies: 5
    Last Post: 08-27-2010, 08:43 PM
  3. java.lang.NullPointerException
    By Pombi in forum New To Java
    Replies: 6
    Last Post: 05-15-2010, 03:12 PM
  4. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-27-2009, 12:36 PM
  5. java.lang.NullPointerException
    By stevemcc in forum AWT / Swing
    Replies: 2
    Last Post: 02-08-2008, 09:01 AM

Tags for this Thread

Posting Permissions

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