alienA2 19 Posted March 27, 2012 Report Share Posted March 27, 2012 Hi I am getting this error. I do not know why. I suspect it has something to do with the do while loop, where it mentions (myScanner.findInLine(".").charAt(0) == 'Y'); . Please advice. error:Do another? Exception in thread "main" java.lang.NullPointerException at AddGuests.main(AddGuests.java:41) code: /*Do another? Exception in thread "main" java.lang.NullPointerException at AddGuests.main(AddGuests.java:41)*/import java.util.Scanner;import static java.lang.System.in;import static java.lang.System.out;class AddGuests{ public static void main(String args[]) { //Variable declarations Scanner myScanner = new Scanner(in); int whichRoom, numGuests; int guestsIn[]; guestsIn = new int[10]; //The for loop is assigning zero to the guestsIn variable with the array value as the room number... for (int roomNum = 0; roomNum < 10; roomNum++) { guestsIn[roomNum] = 0; } do { out.print("Room number: "); whichRoom = myScanner.nextInt(); out.print("How many guests? "); numGuests = myScanner.nextInt(); guestsIn[whichRoom] = numGuests; out.println(); out.print("Do another? "); //do this while the myscanner variable reads input from user to be Y) } while (myScanner.findInLine(".").charAt(0) == 'Y'); //heading to make it look pretty... out.println(); out.println("Room\tGuests"); for (int roomNum = 0; roomNum < 10; roomNum++) { out.print(roomNum); out.print("\t"); out.println(guestsIn[roomNum]); } }} Quote Link to post Share on other sites
alienA2 19 Posted March 27, 2012 Author Report Share Posted March 27, 2012 Me again. Modified the code...it seems to work but your opinions are appreciated: /*Do another? Exception in thread "main" java.lang.NullPointerException at AddGuests.main(AddGuests.java:41)*/import java.util.Scanner;import static java.lang.System.in;import static java.lang.System.out;import java.io.File;import java.io.FileNotFoundException;import java.io.PrintStream;class AddGuests{ public static void main(String args[]) throws FileNotFoundException { //Variable declarations Scanner myScanner = new Scanner(in); PrintStream w2d = new PrintStream("GuestList_Report.xls"); int whichRoom, numGuests; int guestsIn[]; char antwoord; guestsIn = new int[10]; //The for loop is assigning zero to the guestsIn variable with the array value as the room number... for (int roomNum = 0; roomNum < 10; roomNum++) { guestsIn[roomNum] = 0; } do { out.print("Room number: "); whichRoom = myScanner.nextInt(); out.print("How many guests? "); numGuests = myScanner.nextInt(); guestsIn[whichRoom] = numGuests; out.println(); out.print("Do another? "); antwoord = myScanner.next(".").charAt(0); //do this while the myscanner variable reads input from user to be Y) } while (antwoord == 'Y' || antwoord == 'y'); out.println("Thank you. Your information has been logged."); //heading to make it look pretty... w2d.println(); w2d.println("Room\tGuests"); for (int roomNum = 0; roomNum < 10; roomNum++) { w2d.print(roomNum); w2d.print("\t"); w2d.println(guestsIn[roomNum]); } }} Quote Link to post Share on other sites
smiles 7 Posted March 27, 2012 Report Share Posted March 27, 2012 do { out.print("Room number: "); whichRoom = myScanner.nextInt(); out.print("How many guests? "); numGuests = myScanner.nextInt(); guestsIn[whichRoom] = numGuests; out.println(); out.print("Do another? "); // do this while the myscanner variable reads input from user to be // Y)} while (!"Y".equals(myScanner.next().toUpperCase())); Quote Link to post Share on other sites
alienA2 19 Posted March 27, 2012 Author Report Share Posted March 27, 2012 do { out.print("Room number: "); whichRoom = myScanner.nextInt(); out.print("How many guests? "); numGuests = myScanner.nextInt(); guestsIn[whichRoom] = numGuests; out.println(); out.print("Do another? "); // do this while the myscanner variable reads input from user to be // Y)} while (!"Y".equals(myScanner.next().toUpperCase())); do { out.print("Room number: "); whichRoom = myScanner.nextInt(); out.print("How many guests? "); numGuests = myScanner.nextInt(); guestsIn[whichRoom] = numGuests; out.println(); out.print("Do another? "); // do this while the myscanner variable reads input from user to be // Y)} while (!"Y".equals(myScanner.next().toUpperCase())); Why } while (!"Y".equals(myScanner.next().toUpperCase()));Why is the ! character added to the code??? Quote Link to post Share on other sites
smiles 7 Posted March 27, 2012 Report Share Posted March 27, 2012 ! means NOT like !0 = 1 so if "Y".equals(myScanner.next().toUpperCase()) returns true then the condition in 'while' still be false and the loop continues Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.