Jump to content
Sign in to follow this  
alienA2

Do another? Exception in thread "main" java.lang.NullPointerException at AddGuests.main(AddGuests.java:41)

Recommended Posts

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]); } }}

Share this post


Link to post
Share on other sites

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]); } }}

Share this post


Link to post
Share on other sites
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()));

:)

Share this post


Link to post
Share on other sites
 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???

Share this post


Link to post
Share on other sites

! 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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...