The Artima Developer Community
Sponsored Link

Java Answers Forum
"main" java.lang.NullPointerException

8 replies on 1 page. Most recent reply: Jan 8, 2012 9:31 AM by leo kakios

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
leo kakios

Posts: 5
Nickname: namadin
Registered: Jan, 2012

"main" java.lang.NullPointerException Posted: Jan 6, 2012 12:00 PM
Reply to this message Reply
Advertisement
Hi am new here and also in java lang programming so i compile the code but there is an error which i cant see please help :P


import java.util.Scanner;


class NicePrice {


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("Do you 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("Ti?");
}

System.out.print("Please pay $");
System.out.print(price);
System.out.print(". ");
System.out.println("Geia Xara!");

}
}



With error {
How old are you ? 21
Do you have a coupon ? (Y/N) Exception in thread "main" java.lang.NullPointerException
at NicePrice.main(NicePrice.java:18)

Process completed.
}


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: "main" java.lang.NullPointerException Posted: Jan 6, 2012 1:34 PM
Reply to this message Reply
The line where the error occurs appears to have originally been two separate lines that have become joined together badly.

leo kakios

Posts: 5
Nickname: namadin
Registered: Jan, 2012

Re: "main" java.lang.NullPointerException Posted: Jan 6, 2012 3:57 PM
Reply to this message Reply
:S didnt understand you there m8 please explain to me once more when u can ... thanks a lot

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: "main" java.lang.NullPointerException Posted: Jan 6, 2012 11:22 PM
Reply to this message Reply
OK. Let's start again. Did you write this code yourself?

leo kakios

Posts: 5
Nickname: namadin
Registered: Jan, 2012

Re: "main" java.lang.NullPointerException Posted: Jan 7, 2012 2:02 AM
Reply to this message Reply
Goodmorning ;p.

No i didnt write it my self its a pratctise code in a book_for dummies_java_programming.pdf

Copied the code exactly how it was and it didnt run properly
made an other programm which uses "char " not only "int" and the problem was the same ....

George B

Posts: 24
Nickname: hmgeorge
Registered: Feb, 2008

Re: "main" java.lang.NullPointerException Posted: Jan 7, 2012 7:36 AM
Reply to this message Reply
I tried running this and it ran fine for me.

The only way I could reproduce the problem was to hit ctrl-d after the second prompt. If I gave it valid input it worked fine:

$ java NicePrice
How old are you ? 21
Do you have a coupon(Y/N)y
Please pay $7.25. Geia Xara!
$

I'm running on a Linux box. My guess is that maybe it has problems running under Windows? Maybe the <cr><lf> line termination is giving it trouble?

leo kakios

Posts: 5
Nickname: namadin
Registered: Jan, 2012

Re: "main" java.lang.NullPointerException Posted: Jan 7, 2012 10:01 AM
Reply to this message Reply
A fast debug brought me here before this line

System.out.print("Do you have a coupon ? (Y/N) ");
reply = myScanner.findInLine(".").charAt(0); <=== this line has error

Debug jumped me here at Thread.java in this line


private void dispatchUncaughtException(Throwable e) {
getUncaughtExceptionHandler().uncaughtException(this, e);
}

i have downloaded the java api doc etc from an old link ... dunno m8 if my win7 have the problem am not pro on this stuff plz whenever you have time send me a quick link for java doc .zip /.rar i might have problem in the doc files :/


THANKS A LOT
cant find them googled them everywere

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: "main" java.lang.NullPointerException Posted: Jan 8, 2012 5:19 AM
Reply to this message Reply
Ah, OK. The example program is bad in very many ways (even if you exclude the fact that it doesn't work).

The simplest way to get this code to work is to replace
.findInLine(".")
with
.next()
(The code gives the same error both in Windows and Mac. The error is down to the author not using the Scanner object properly to process keyboard input.)

leo kakios

Posts: 5
Nickname: namadin
Registered: Jan, 2012

Re: "main" java.lang.NullPointerException Posted: Jan 8, 2012 9:31 AM
Reply to this message Reply
Omg it worked.... i can finaly continue ouf...

A Big BIG THANKS.... finaly i was thinking that api had problems etc :P a big relief :P

Thanks a lot again. +1


░░░░░░░░░░░░▄▄
░░░░░░░░░░░█░░█
░░░░░░░░░░░█░░█
░░░░░░░░░░█░░░█
░░░░░░░░░█░ ░░░█
███████▄▄█░░░░░██████▄
▓▓▓▓▓▓█░░░░░░░░░░░░░░█
▓▓▓▓▓▓█░░░░░░░░░░░░░░█
▓▓▓▓▓▓ █░░░░░░░░░░░░░░█
▓▓▓▓▓▓█░░░░░░░░░░░░░░█
▓▓▓▓▓▓█░░░░░░░░░░░░░░█
▓▓▓▓▓▓█████░░░░░░ ░░░█
██████▀░░░░▀▀██████▀

Flat View: This topic has 8 replies on 1 page
Topic: get Class and Method name of calling class Previous Topic   Next Topic Topic: Query on java servlet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use