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 12 of 12

Thread: Problem with java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with java.lang.NullPointerException

    Hello all and thank you for keeping this forum up and running,
    I am new to this and only just introduced myself here.

    However, I have my first problem at hand. Trying to run some simple if statements (I am a noobie to Java), I ran into this error:

    --------------------Configuration: AnotherAgeCheck - JDK version 1.7.0_01 <Default> - <Default>--------------------
    How old?65
    Coupon?Exception in thread "main" java.lang.NullPointerException
    at AnotherAgeCheck.main(AnotherAgeCheck.java:15)

    Process completed.
    My code was as follows:

    import java.util.Scanner;
     
     
    class AnotherAgeCheck {
    	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?");
    		age = myScanner.nextInt();
     
    		System.out.print("Coupon?");
    		reply = myScanner.findInLine(".").charAt(0);
     
    		if (age >= 12 && age < 65) {
    			price=9.25;
    			if (reply == 'Y' || reply == 'y') {
    				price -= 2.00;
    			}
    		}else {
    			price = 5.25;
    		}
    		System.out.print("Pay $ ");
    		System.out.println(price);
    		System.out.println("Enjoy!");
    		}
    	}

    I really don't get what's missing. Can somebody help me out?

    Thank you very much


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Problem with java.lang.NullPointerException

    Well, after looking at your code,
    1. what do you expect findInLine() to return?
    2. Do you have the complete understanding how this function works?
    I would suggest you readingScanner (Java 2 SE Platform 5.0)

  3. #3
    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: Problem with java.lang.NullPointerException

    Exception in thread "main" java.lang.NullPointerException
    at AnotherAgeCheck.main(AnotherAgeCheck.java:15)
    The error message says that a variable on line 15 has a null value. You need to look at line 15 and find which variable is null and then backtrack in your code to find out why that variable does not have a valid value. To see which variable is null, add a println statement just before line 15 and print out the values of all the variables used on line 15.

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with java.lang.NullPointerException

    Quote Originally Posted by Mr.777 View Post
    Well, after looking at your code,
    1. what do you expect findInLine() to return?
    2. Do you have the complete understanding how this function works?
    I would suggest you readingScanner (Java 2 SE Platform 5.0)
    Hello, like I said, I am a noobie. However, I want the scanner to find the next entered letter and assign it to the variable 'reply'. Do I need to initiate the variable 'reply' first?

    Thank you

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Problem with java.lang.NullPointerException

    No, you don't want to initiate it but to declare it. Initialization will be optional in this case as you want user to input this. So, whatever value you initialize it with, it will be over written by user given value. But you must declare it, as it's a common rule.

  6. #6
    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: Problem with java.lang.NullPointerException

    reply = myScanner.findInLine(".").charAt(0);
    What values can the findInLine() method return? Read the API doc to see.
    You need to test that it has a valid value BEFORE calling the charAt() method for the value returned.

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with java.lang.NullPointerException

    Quote Originally Posted by Mr.777 View Post
    No, you don't want to initiate it but to declare it. Initialization will be optional in this case as you want user to input this. So, whatever value you initialize it with, it will be over written by user given value. But you must declare it, as it's a common rule.
    I thought I had declared the var "reply" by 'char reply;' ? So this shouldn't be the problem, I guess.


    Quote Originally Posted by Norm View Post
    What values can the findInLine() method return? Read the API doc to see.
    You need to test that it has a valid value BEFORE calling the charAt() method for the value returned.

    I tried to read the API, however, I have to admit I cannot seem to be able to make sense out of it in this case. I want the findInLine() method to find the next Character. According to the book I am reading, (".") should make it find the next Character input. I have been trying to use the findInLine method together with the charAt method according to this:
    http://www.uop.edu.jo/e-courses/Begi...va%20(128).pdf

    However, I get the NullPointerException, the user doesn't even have a chance of inputting anything the second time. I am working with jdk 7. Is there a chance this method does not work anymore with the current version? If so, which method could I use to read a single char from the keyboard?

    And again: thank you very much for your pointers.
    Last edited by more_dread; November 17th, 2011 at 11:47 PM.

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Problem with java.lang.NullPointerException

    There is no datatype as var in Java. (Remember Java and Javascript are not same)
    And char reply; is fine. Now you can input a single character in this variable.
    What Norm means is, you must check before using findInLine() that the String you are applying at is not null at all. And when you do,
    Scanner obj = new Scanner(System.in);
    It means you will get it from the keyboard but you are actually passing nothing to Scanner constructor.
    findInLine() needs an expression to match to the String passed to Scanner's constructor (which in your case is null).
    obj.findInLine(".").charAt(0);
    you want it to find from null.


    Why don't you try using
    BufferedRead obj1=new BufferedReader(new InputStreamReader(System.in));
    or
    Console obj;

    I want you to read it carefully.

  9. #9
    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: Problem with java.lang.NullPointerException

    Does the API doc for findInLine() say that it can return a null value?
    Is it doing that in your code?

  10. #10
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with java.lang.NullPointerException

    Quote Originally Posted by Norm View Post
    Does the API doc for findInLine() say that it can return a null value?
    Is it doing that in your code?
    No it does not say if it can return a Null value. Sorry, I cannot wrap my head around this. with the "Scanner.findInLine()" should the program not be waiting for an input? It does not even give the user the chance to enter an input.

  11. #11
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Problem with java.lang.NullPointerException

    It will ofcourse not. I told you the reason earlier. Read my post.

  12. #12
    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: Problem with java.lang.NullPointerException

    No it does not say if it can return a Null value
    public String findInLine(Pattern pattern)
    Attempts to find the next occurrence of the specified pattern ignoring delimiters. If the pattern is found before the next line separator, the scanner advances past the input that matched and returns the string that matched the pattern. If no such pattern is detected in the input up to the next line separator, then null is returned and the scanner's position is unchanged. This method may block waiting for input that matches the pattern.
    From the API doc.

Similar Threads

  1. [SOLVED] java.lang.NullPointerException
    By macko in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 3rd, 2011, 10:39 AM
  2. java.lang.NullPointerException
    By mwr76 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 8th, 2011, 04:39 PM
  3. java.lang.nullPointerException
    By ridg18 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 25th, 2010, 03:52 PM
  4. problem in java.lang.NullPointerException
    By jianghuzai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 28th, 2010, 10:24 AM
  5. java.lang.NullPointerException - Help
    By mds1256 in forum Exceptions
    Replies: 5
    Last Post: November 30th, 2009, 06:31 PM