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:
Quote:
--------------------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:
Code :
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
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)
Re: Problem with java.lang.NullPointerException
Quote:
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.
Re: Problem with java.lang.NullPointerException
Quote:
Originally Posted by
Mr.777
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 reading
Scanner (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
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.
Re: Problem with java.lang.NullPointerException
Quote:
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.
Re: Problem with java.lang.NullPointerException
Quote:
Originally Posted by
Mr.777
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
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.
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,
Code :
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).
Code :
obj.findInLine(".").charAt(0);
you want it to find from null.
Why don't you try using
Code :
BufferedRead obj1=new BufferedReader(new InputStreamReader(System.in));
or
I want you to read it carefully.
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?
Re: Problem with java.lang.NullPointerException
Quote:
Originally Posted by
Norm
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.
Re: Problem with java.lang.NullPointerException
It will ofcourse not. I told you the reason earlier. Read my post.
Re: Problem with java.lang.NullPointerException
Quote:
No it does not say if it can return a Null value
Quote:
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.