Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: NullPointerException trouble

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NullPointerException trouble

    Hi guys,

    I've been trying for a while to solve that problem, but it just gave me headache. Really don't understand why it's not working.

    I got this message each time:
    Have a coupon? (Y/N) Exception in thread "main" java.lang.NullPointerException
    at DescuentoPeli.main(DescuentoPeli.java:14)

    He you have the whole code. Please, be kind to help me.

    import java.util.Scanner;

    class DescuentoPeli {

    public static void main(String args[]) {
    Scanner myScanner = new Scanner(System.in);
    int age;
    double price = 0.00;
    char reply;

    System.out.print("How old are you? ");
    age = myScanner.nextInt();
    System.out.print("Have a coupon? (Y/N) ");
    reply = myScanner.findInLine(".").charAt(0);

    if (age >= 12 && age < 65) {
    price = 9.25;
    }

    if (age < 12 || age >= 65) {
    price = 5.25;
    }

    if (reply == 'Y' || reply == 'y') {
    price -= 2.00;
    }

    if (reply != 'Y' && reply != 'y' && reply!= 'N' && reply!= 'n') {
    System.out.println("Huh?");
    }

    System.out.print("Please pay $");
    System.out.print(price);
    System.out.print(". ");
    System.out.println("Enjoy the show!");
    }
    }[/I]


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: NullPointerException trouble

    java.lang.NullPointerException
    at DescuentoPeli.main(DescuentoPeli.java:14)
    What variable on line 14 has a null value? Then backtrack in the code to see why it does not have a valid, non null value.

    Break any compound method call statements up into single method call statements and test the value returned by each method BEFORE you use that value for a further method call.

    A compound method call would look like this:
    method1().method2().method3()
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. NullPointerException
    By deathmatex in forum Exceptions
    Replies: 4
    Last Post: March 27th, 2012, 03:54 AM
  2. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:15 PM
  3. [SOLVED] Nullpointerexception
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 14th, 2010, 10:33 PM
  4. [SOLVED] NullPointerException
    By javapenguin in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2010, 12:10 AM
  5. NullPointerException
    By bbr201 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2010, 07:06 PM

Tags for this Thread