Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36090

Re: i need help figuring out what is wrong in my code

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: i need help figuring out what is wrong in my code
Date 2016-12-12 11:27 -0500
Organization A noiseless patient Spider
Message-ID <o2mj2n$b1d$1@dont-email.me> (permalink)
References <6f8c0f7d-a8b2-4e50-b815-d5d77e6e25d5@googlegroups.com> <f974c0f7-ba33-481f-a97e-b4c75e0f011b@googlegroups.com>

Show all headers | View raw


On 12/12/2016 11:15 AM, Karim omar wrote:
> On Monday, December 12, 2016 at 7:13:46 PM UTC+3, 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();

     This consumes and converts the number (or throws an exception if
the user enters something non-numeric), and stops reading as soon as
it has swallowed the number.  It does not swallow anything after the
number -- in particular, it does not swallow the end-of-line marker.

> 		System.out.println("you have a coupon? (Y/N)");
> 		reply = myScanner.findInLine(".").charAt(0);

     This looks for a match in the current line.  If the number from
the earlier call was the last thing on the line, and the end-of-line
marker has not yet been consumed, then the remainder of the current
line is empty.  The findInLine() method will therefore fail to find
anything, and will return null.  And when you attempt null.charAt(0),
you get a NullPointerException.

-- 
esosman@comcast-dot-net.invalid
"Nobody ever went broke underestimating the intelligence of the
American public." -- HLM (paraphrased)

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

i need help figuring out what is wrong in my code Karim omar <karimyousrymohamedomar@gmail.com> - 2016-12-12 08:13 -0800
  Re: i need help figuring out what is wrong in my code Karim omar <karimyousrymohamedomar@gmail.com> - 2016-12-12 08:15 -0800
    Re: i need help figuring out what is wrong in my code Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-12-12 11:27 -0500
  Re: i need help figuring out what is wrong in my code Karim omar <karimyousrymohamedomar@gmail.com> - 2016-12-12 08:54 -0800
    Re: i need help figuring out what is wrong in my code Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-12-12 12:02 -0500
      Re: i need help figuring out what is wrong in my code Karim omar <karimyousrymohamedomar@gmail.com> - 2016-12-12 09:06 -0800
        Re: i need help figuring out what is wrong in my code Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-12-12 12:40 -0500
          Re: i need help figuring out what is wrong in my code Karim omar <karimyousrymohamedomar@gmail.com> - 2016-12-12 09:52 -0800
            Re: i need help figuring out what is wrong in my code Eric Douglas <e.d.programmer@gmail.com> - 2016-12-12 10:21 -0800
            Re: i need help figuring out what is wrong in my code Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-12-12 13:36 -0500
              Re: i need help figuring out what is wrong in my code Karim omar <karimyousrymohamedomar@gmail.com> - 2016-12-12 12:28 -0800
                Re: i need help figuring out what is wrong in my code Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-12-12 16:28 -0500
                Re: i need help figuring out what is wrong in my code Karim omar <karimyousrymohamedomar@gmail.com> - 2016-12-12 13:39 -0800
                Re: i need help figuring out what is wrong in my code Karim omar <karimyousrymohamedomar@gmail.com> - 2016-12-12 14:17 -0800
                Re: i need help figuring out what is wrong in my code Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-12-12 18:08 -0500
      Re: i need help figuring out what is wrong in my code Karim omar <karimyousrymohamedomar@gmail.com> - 2016-12-12 09:09 -0800
  Re: i need help figuring out what is wrong in my code lewbloch@gmail.com - 2016-12-13 12:38 -0800
    Re: i need help figuring out what is wrong in my code Eric Douglas <e.d.programmer@gmail.com> - 2016-12-13 12:49 -0800
      Re: i need help figuring out what is wrong in my code lewbloch@gmail.com - 2016-12-14 17:01 -0800
        Re: i need help figuring out what is wrong in my code Martin Gregorie <martin@address-in-sig.invalid> - 2016-12-15 10:47 +0000
  Re: i need help figuring out what is wrong in my code Roedy Green <inquiry@mindprod.com> - 2016-12-13 16:07 -0800

csiph-web