Results 1 to 3 of 3
Thread: NullPointerException help?
- 02-04-2010, 04:44 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 1
- Rep Power
- 0
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.
- 02-04-2010, 05:06 PM #2
Member
- Join Date
- Dec 2009
- Posts
- 36
- Rep Power
- 0
This will probally return null:
myScanner.findInLine(".")
make sure ur Scanner object is also not null
- 02-04-2010, 08:26 PM #3
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
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.Java Code:System.out.print("Have a coupon? (Y/N) "); ------------------------------------- reply = myScanner.findInLine(".").charAt(0); -------------------------------------
Instead try using Scanner.nextLine() or Scanner.next.Attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
This does the job but got some minor issues.:)Java Code:String whatever = Scanner.nextLine(); //and check whether that String is Y or N
Similar Threads
-
NullPointerException I NEED HELP
By mayhewj7 in forum New To JavaReplies: 2Last Post: 02-13-2009, 08:03 AM -
I get a NullPointerException and don't know why
By hendrix79 in forum New To JavaReplies: 9Last Post: 12-14-2008, 06:18 AM -
NullPointerException
By Aika in forum New To JavaReplies: 8Last Post: 11-18-2008, 11:34 PM -
NullPointerException
By adeeb in forum AWT / SwingReplies: 3Last Post: 06-11-2008, 08:42 AM -
NullPointerException
By Feng in forum New To JavaReplies: 5Last Post: 11-24-2007, 07:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks