#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Java Variable Error


    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)
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,534
    Rep Power
    1906
    price is only assigned in if-block. You should give it a default value:
    Code:
    double price = 0.0;
    Regarding NPE at line 18, you can use next() instead of findInLine():
    Code:
    reply = myScanner.next().charAt(0);
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0
    Originally Posted by MrFujin
    price is only assigned in if-block. You should give it a default value:
    Code:
    double price = 0.0;
    Regarding NPE at line 18, you can use next() instead of findInLine():
    Code:
    reply = myScanner.next().charAt(0);
    Huh never thought of that. Wonder why my book doesn't mention it. Might be because it's from 2005 or something. Will give it a shot tomorrow since i am away from my main computer.

IMN logo majestic logo threadwatch logo seochat tools logo