Hi guys, I've just started trying to teach myself Java less than two weeks ago. I've been reading the "Beginning Programming Java for Dummies" and I've writing small things to practice as I go that review what I've learned so far. I tried to make a small program that checked your age to see if you could buy cigarettes, asked what kind you'd like, ask how many, and then give you a total price. It compiles fine but after the cigarette selection there's an error and it terminates itself and I don't know why.
This is the error that shows up
Exception in thread "main" java.lang.NullPointerException
at CigarettesOrNo.main(CigarettesOrNo.java:56)
and this is the code
import java.util.Scanner; import static java.lang.System.out; public class CigarettesOrNo { public static void main(String args[]) { Scanner myScanner = new Scanner(System.in); int month, day, year; char reply,sure; double marlboro = 5.95; double newport = 6.49; double generic = 4.39; int whichCigarette; int howMany; out.println("What is your birthday? Please type as follows MM DD YYYY"); month = myScanner.nextInt(); day = myScanner.nextInt(); year = myScanner.nextInt(); if (year < 1993) { reply = 'y'; } else if (year == 1993 && month < 4) { reply = 'y'; } else if (year == 1993 && month == 4 && day < 27) { reply = 'y'; } else { reply = 'n'; } if (reply == 'y') { out.println("Which cigarettes would you like? 1. Marlboro, 2. Newport, or 3. Generics? Please select a number."); whichCigarette = myScanner.nextInt(); if (whichCigarette == 1) { out.println("Marlboros are $5.99 per pack, is that alright? (Y/N)"); sure = myScanner.findInLine(".").charAt(0); if (sure == 'y') { out.println("How many packs would you like?"); howMany = myScanner.nextInt(); out.print("Your total will be $" + marlboro * howMany + ". Thank you for your purchase!"); } else { out.println("Thanks for looking."); } } if (whichCigarette == 2) { out.println("Newports are $6.49 per pack, is that alright? (Y/N)"); sure = myScanner.findInLine(".").charAt(0); if (sure == 'y') { out.println("How many packs would you like?"); howMany = myScanner.nextInt(); out.print("Your total will be $" + newport * howMany + ". Thank you for your purchase!"); } else { out.println("Thanks for looking."); } } if (whichCigarette == 3) { out.println("Generics are $4.39 per pack, is that alright? (Y/N)"); sure = myScanner.findInLine(".").charAt(0); if (sure == 'y') { out.println("How many packs would you like?"); howMany = myScanner.nextInt(); out.print("Your total will be $" + generic * howMany + ". Thank you for your purchase!"); } else { out.println("Thanks for looking."); } } } else { out.println("I'm sorry, you are too young to buy cigarettes!"); } } }
The error points to this line of code "sure = myScanner.findInLine(".").charAt(0);" but I don't know what's wrong with it. Maybe I'm too much of a beginner to see it haha. Anyway, thanks to anyone that can help me