Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Beginner problem with a do-while.

  1. #1
    Junior Member Eliijahh's Avatar
    Join Date
    Nov 2011
    Location
    Rome Italy
    Posts
    2
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginner problem with a do-while.

    do {
    out.print("Which room to modify? ");
    roomNum = myScanner.nextInt();
    out.print("How many guests inside? ");
    numGuests = myScanner.nextInt();
    guestsIn[roomNum] = numGuests;
    out.print("Do you want to modify another room? (Y/N)");
    reply = myScannerReply.findInLine(".").charAt(0);
    } while (reply == 'y' || reply == 'Y');

    This is just a little exercise program that I'm doing to learn how to use java. (I'm studying on java for dummies, then I'll try to find something more complex )

    Anyway I just don't understand why this do-while works the first time, then when it arrives at the
    "Do you want to modify another room?" a NullPointerException error appears, ending the program.

    Which room to modify? 3
    How many guests inside? 2
    Do you want to modify another room? (Y/N)y
    Which room to modify? 2
    How many guests inside? 3
    Exception in thread "main" java.lang.NullPointerException
    at FlexibleHotelRooms.main(FlexibleHotelRooms.java:28 )
    Do you want to modify another room? (Y/N)Java Result: 1

    Do you know why?
    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Beginner problem with a do-while.

    Exception in thread "main" java.lang.NullPointerException
    at FlexibleHotelRooms.main(FlexibleHotelRooms.java:28 )
    The error message says you are trying to use a variable with a null value on line 28.
    You need to look at line 28 and see what variable is null and then backtrack to see why that variable does not have a valid value.
    If you can not see what variable is null, add a println statement just before line 28 that prints out the values of all the variables used in line 28.

    Also it is possible for methods to return null values so check if any of them that you are using are returning a null value.

  3. #3
    Junior Member Eliijahh's Avatar
    Join Date
    Nov 2011
    Location
    Rome Italy
    Posts
    2
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner problem with a do-while.

    If you can not see what variable is null, add a println statement just before line 28 that prints out the values of all the variables used in line 28.
    Ok I've done that. I called the reply var '.' first outside the do-while. So at first it gives me back a ".", so I write "Y" to assign to that char var the "Y" value. Then the second round of the loop the println at the line before the 28 gives me back a Y, which should be correct since in the while(conditions) I've put 'Y' and 'y' to continue running the do-while, but still it gives me back the null error. I can't understand what I did wrong.

    Also it is possible for methods to return null values so check if any of them that you are using are returning a null value.
    What do you mean? I can't understand

    Thanks for the anwser anyway.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Beginner problem with a do-while.

    reply = myScannerReply.findInLine(".").charAt(0);
    Break this statement into two steps and test what is returned by the findInLine() method to make sure it is not null before calling the charAt() method.

Similar Threads

  1. beginner needs help with simple problem
    By ddonn in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 21st, 2011, 12:16 PM
  2. Beginner programming- strange problem
    By jkkruse in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 23rd, 2011, 06:39 AM
  3. Beginner Problem
    By jokibaer in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 15th, 2011, 07:34 PM
  4. Beginner Problem
    By Melvrick in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 15th, 2011, 01:29 PM
  5. Beginner Problem
    By nve5009 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 19th, 2010, 11:29 AM