Results 1 to 3 of 3
  1. #1
    fab5freddy is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default NullPointerException help?

    I am fairly new to java. I have only been working with it for about two weeks. I'm trying to teach myself as I go to see what it's like before I try to enroll in any classes. The code I am writing is meant to tell a person how much to pay if they were to go to the movies and also if they have a coupon or not. It does this by asking your age and if you have a coupon or not. However each time I run the code I'm able to type in the age, but when it ask if you have a coupon or not I get the error message. "Exception in thread "main" java.lang.NullPointerException at YouCanDoIt.main(YouCanDoIt.java:16)" Here is a listing of my code:

    import java.util.Scanner;

    class YouCanDoIt {

    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!");
    }
    }
    The more detail you can provide would help me greatly as I said I am quite new to this.

  2. #2
    jojo is offline Member
    Join Date
    Dec 2009
    Posts
    36
    Rep Power
    0

    Default

    This will probally return null:
    myScanner.findInLine(".")

    make sure ur Scanner object is also not null

  3. #3
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    Java Code:
    System.out.print("Have a coupon? (Y/N) ");
    
    -------------------------------------
    reply = myScanner.findInLine(".").charAt(0);
    -------------------------------------
    do you know whats findinline doing?Its purpose is not meant to read the users input,its purpose is to read the already specified Strings.
    Attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
    Instead try using Scanner.nextLine() or Scanner.next.

    Java Code:
    String whatever = Scanner.nextLine();
    //and check whether that String is Y or N
    This does the job but got some minor issues.:)

Similar Threads

  1. NullPointerException I NEED HELP
    By mayhewj7 in forum New To Java
    Replies: 2
    Last Post: 02-13-2009, 09:03 AM
  2. I get a NullPointerException and don't know why
    By hendrix79 in forum New To Java
    Replies: 9
    Last Post: 12-14-2008, 07:18 AM
  3. NullPointerException
    By Aika in forum New To Java
    Replies: 8
    Last Post: 11-19-2008, 12:34 AM
  4. NullPointerException
    By adeeb in forum AWT / Swing
    Replies: 3
    Last Post: 06-11-2008, 09:42 AM
  5. NullPointerException
    By Feng in forum New To Java
    Replies: 5
    Last Post: 11-24-2007, 08:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •