Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Knute Johnson Newsgroups: comp.lang.java.help Subject: Re: need help with my code Date: Mon, 12 Dec 2016 15:16:08 -0600 Organization: A noiseless patient Spider Lines: 49 Message-ID: References: <619ae8ec-bcca-40b4-8be8-17dd0a63cb1d@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 12 Dec 2016 21:14:45 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="7ea7e56ce1ce903389ecf1baa8c29907"; logging-data="15149"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+EHHbhnsH6Tc8bEgh1ZW7C" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1 In-Reply-To: <619ae8ec-bcca-40b4-8be8-17dd0a63cb1d@googlegroups.com> Cancel-Lock: sha1:7f0YENEshlkSF5JaIRpJYcX3cD8= Xref: csiph.com comp.lang.java.help:4162 On 12/12/2016 10:19, Karim omar wrote: > when i use the (reply = myScanner.findInLine(".").charAt(0);) method i get the following error message > > Exception in thread "main" java.lang.NullPointerException > at Ifexercise1a.main(Ifexercise1a.java:18) > > and i am sure i wrote it write cause i tried copying and paste it from another prog which is working fine, i am using eclipse and i don`t know if it`s a bug in eclipse or something wrong in the class itself. > > the complete code is > > import java.util.Scanner; > > public class Ifexercise1a { > > public static void main(String[] args) { > > Scanner myScanner = new Scanner(System.in); > int age; > double price; > char reply; > > System.out.println("how old are you?"); > age = myScanner.nextInt(); > > System.out.println("you have a coupon? (Y/N)"); > reply = myScanner.findInLine(".").charAt(0); > > if(age <= 12 || age > 65){ > price = 5.25; > }else{ > price = 9.25; > } > if (reply == 'y' || reply == 'Y'){ > price-=2.00; > } > System.out.println("you will pay $" + price); > } > > > } > I'm not sure what you are trying to do with the line 'reply = myScanner.findInLine(".").charAt(0)' but if you replace the .findInLine() with .next() it will work fine. -- Knute Johnson