Results 1 to 3 of 3
  1. #1
    Beginner's Avatar
    Beginner is offline Member
    Join Date
    Apr 2010
    Posts
    25
    Rep Power
    0

    Default Help with Null Pointer Exception

    Hi guys, this is my 1st post here, but expect me to be active in the following days since I am currently learning how to program Java, and I have been given a book about it, and there is this particular code on the book which I am sure that I have copied exactly, up to every letter, but the problem is it is still showing an error when I scan for a character. Please help, thanks in advance!

    Java Code:
    Scanner myScanner = new Scanner(in);
            int age;
            double price = 0.0;
            char reply;
    
            out.println("How old are you?");
            age = myScanner.nextInt();
    
            out.println("Do you have a coupon? Y/N");
            reply = myScanner.findInLine(".").charAt(0);
    
            if (age >= 12 && age < 65) {
                price = 9.25;
            } else if (age < 12 || age >= 65) {
                price = 5.25;
            }
    
            if (reply == 'y' || reply == 'Y') {
                price -= 2.00;
            } else {
                out.println("Huh?");
            }
    
            out.println("Please pay $" + price +
                    ".");

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're running into one of the trickiest problems with using a java.util.Scanner object: capturing the end-of-line token.

    When you do this:
    Java Code:
        System.out.println("How old are you?");
        age = myScanner.nextInt();
    your Scanner object grabs the next int token but not the next end-of-line token. So then when you do this:


    Java Code:
        System.out.println("Do you have a coupon? Y/N");
        reply = myScanner.findInLine(".").charAt(0);
    The Scanner object grabs the end-of-line token and tries to process it but findInLine() will return null (see the API on this).

    The solution is to explicitly grab the end-of-line token after grabbing the int:

    Java Code:
    [import java.util.Scanner;
    
    public class Fu3 {
      public static void main(String[] args) {
        Scanner myScanner = new Scanner(System.in);
        int age;
        double price = 0.0;
        char reply;
    
        System.out.println("How old are you?");
        age = myScanner.nextInt();
        myScanner.nextLine();  // ***** add this! ****
    
        System.out.println("Do you have a coupon? Y/N");
        reply = myScanner.findInLine(".").charAt(0);
    
        if (age >= 12 && age < 65) {
          price = 9.25;
        } else if (age < 12 || age >= 65) {
          price = 5.25;
        }
    
        if (reply == 'y' || reply == 'Y') {
          price -= 2.00;
        } else {
          System.out.println("Huh?");
        }
    
        System.out.println("Please pay $" + price + ".");
      }
    }

  3. #3
    Beginner's Avatar
    Beginner is offline Member
    Join Date
    Apr 2010
    Posts
    25
    Rep Power
    0

    Default

    It works! Thank you very much! I will try to research about that, perhaps the code on the book is outdated? Anyways, this is a very cool forum btw, in the future when I'm more experienced on java, I'll try to help out others here too. You may lock the thread, thanks again!

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. Null pointer exception
    By talha06 in forum JDBC
    Replies: 5
    Last Post: 07-14-2009, 01:12 AM
  3. Help with null pointer exception
    By gammaman in forum New To Java
    Replies: 4
    Last Post: 07-14-2009, 12:23 AM
  4. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 03:30 AM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 05:56 AM

Posting Permissions

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