Hi all,

I'm very much a beginner in Java and going through an example in a book I purchased I noticed I kept getting this 'Exception in thread "main" java.lang.NullPointException upon running the program.

Note: I use JCreator and am using Java 6, the author of the book I am learning from also uses JCreator however using Java 5.

Simple program to illustrate using multiple 'if' statements along with boolean operators

import java.util.Scanner;

class NicePrice {
public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
int age;
double price = 0.00;
char reply;
boolean isKid, isSenior, hasCoupon, hasNoCoupon;
System.out.print(“How old are you? “);
age = myScanner.nextInt();
System.out.print(“Have a coupon? (Y/N) “);
reply = myScanner.findInLine(“.”).charAt(0);
isKid = age < 12;
isSenior = age >= 65;
hasCoupon = reply == ‘Y’ || reply == ‘y’;
hasNoCoupon = reply == ‘N’ || reply == ‘n’;

if (!isKid && !isSenior) {
price = 9.25;
}
if (isKid || isSenior) {
price = 5.25;
}
if (hasCoupon) {
price -= 2.00;
}
if (!hasCoupon && !hasNoCoupon) {
System.out.println(“Huh?”);
}
System.out.print(“Please pay $”);
System.out.print(price);
System.out.print(“. “);
System.out.println(“Enjoy the show!”);
}
}

Now when I run this program I receive that error. However, when I swap the lines:

System.out.print(“How old are you? “);
age = myScanner.nextInt();

System.out.print(“Have a coupon? (Y/N) “);
reply = myScanner.findInLine(“.”).charAt(0);

to

System.out.print(“Have a coupon? (Y/N) “);
reply = myScanner.findInLine(“.”).charAt(0);

System.out.print(“How old are you? “);
age = myScanner.nextInt();

Of course the author runs into no problems with his program, so my question is: Did something change between Java 5 and 6? Am I doing this wrong?