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 :)
Re: Beginner problem with a do-while.
Quote:
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.
Re: Beginner problem with a do-while.
Quote:
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.
Quote:
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. :)
Re: Beginner problem with a do-while.
Quote:
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.