Hi, I'm having a problem with reading from the char , coupon, and it comes up with an error saying

Exception in thread "main" java.lang.NullPointerException
        at input.Main.main(Main.java:27)

At the line which I read from the char, which is that myScan.findInLine bit, I am working from a beginning java programming book, and I'm pretty sure everything is the same but I still receive that error when I get up to that bit in the program(after it asks Do you have a coupon?)

public class Main {

    public static void main(String[] args) {
        int age = 0;
        char coupon;
        double price = 0;
       Scanner myScan = new Scanner(System.in);
      System.out.println("How old are you?");
      age = myScan.nextInt();
       System.out.println("Do you have a coupon?(Y/N)");
       coupon = myScan.findInLine(".").charAt(0);
       System.out.println(coupon);


       if (age >= 18 && age<=55 )
       {
               price = 11.25;
        }
        else
        {
           price = 9.25;
        }


        if(coupon=='y'||coupon=='Y') {
             price = price - 2.00;
         }
         else {
           System.out.println("No Coupon. Price Remains the same.");
         }

       System.out.println("The price will be:" + price);

Well
If you replace the place of this piece of code:

System.out.println("Do you have a coupon?(Y/N)");
            coupon = myScan.findInLine(".").charAt(0);
            System.out.println(coupon);

in the place of this piece of code :

System.out.println("How old are you?");
            age = myScan.nextInt();

to let your program be like this :

public static void main(String[] args) {
            int age = 0;
            char coupon;
            double price = 0;
            Scanner myScan = new Scanner(System.in);

            System.out.println("Do you have a coupon?(Y/N)");
            coupon = myScan.findInLine(".").charAt(0);
            System.out.println(coupon);

            System.out.println("How old are you?");
            age = myScan.nextInt();



            if (age >= 18 && age <= 55) {
                price = 11.25;
            } else {
                price = 9.25;
            }


            if (coupon == 'y' || coupon == 'Y') {
                price = price - 2.00;
            } else {
                System.out.println("No Coupon. Price Remains the same.");
            }

            System.out.println("The price will be:" + price);
        }

It will work !!
don't ask me why ... because I found it by trial and error !!
Some one tell us why??

Woah yea iit does work! But anyways, why?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.