Hey there, so I'm 16 and decided I wanted to teach myself Java after some success with other languages, I picked up a book to help me get on my way. While doing one of the exercises I came across a problem and made sure I had done the code correctly in the book, it appears I have but an error is still popping up.
Here's the code:
Code:
package ticketpricewithdiscount;
import java.util.Scanner;
public class TicketPriceWithDiscount {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
int age;
double price;
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 the error comes up at the
"System.out.print(price)" as price has a red line underneath it and the editor's reasoning to do this was:
"Initialize Variable price"
I thought I had already done that personally. Anyway, whatever help I could get is appreciated since I don't want to move on in case this pops up further down the road.
(I also apologise for what is probably stupidity.)
EDIT:
When I try to run it this comes up
Exception in thread "main" java.lang.NullPointerException
at ticketpricewithdiscount.TicketPriceWithDiscount.main(TicketPriceWithDiscount.java:18)