alienA2 Posted March 27, 2012 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]); } }} Link to comment Share on other sites More sharing options...
alienA2 Posted March 27, 2012 Author 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]); } }} Link to comment Share on other sites More sharing options...
smiles Posted March 27, 2012 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())); Link to comment Share on other sites More sharing options...
alienA2 Posted March 27, 2012 Author 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??? Link to comment Share on other sites More sharing options...
smiles Posted March 27, 2012 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 Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.