-1

It's working fine in IDE but got null pointer exception on Codechef.

on the second line "parseFloat" it's getting null pointer exception

can anyone solve this issue?

            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            float am = Float.parseFloat(br.readLine());
            float bal = Float.parseFloat(br.readLine());
            BufferedWriter ot = new BufferedWriter(new OutputStreamWriter(System.out));
            if((am <= bal) && am%5 == 0) {
                float ball = bal - am;
                ball -= 0.50;
                String.format("%.5f", ball);
                ot.write(String.valueOf(bal));
            }
  • 1
    [dovumentation](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/BufferedReader.html#readLine()) of `readLine()`: "**Returns:** ... null if the end of the stream has been reached" (usually input from IDE is not closed automatically, while probably that site is closing it) – user16320675 Jan 11 '22 at 07:10
  • 1
    Because there is no line to read => it returns null – luk2302 Jan 11 '22 at 07:10
  • But why? It's taking input on IDE but not on Codechef. Can you tell me where I can make some changes? – Sarthak Kumar Mishra Jan 11 '22 at 07:15
  • @user16320675 can you tell me what I can do to make it work on Codechef? – Sarthak Kumar Mishra Jan 11 '22 at 07:16
  • @user16320675 ok I'm checking on what you said – Sarthak Kumar Mishra Jan 11 '22 at 07:20
  • Why would anyone want to wrap `System.out` in a `BufferedWriter` and then later write `ot.write(String.valueOf(bal));` instead of directly using `System.out.print(bal);`? – Thomas Kläger Jan 11 '22 at 07:42
  • My guess is that you have not implemented the requirements correctly. But since you haven't told us what the (complete) requirements are, this is only a guess. – Stephen C Jan 11 '22 at 08:28

1 Answers1

0

You should give two lines via the site. @user16320675 mentioned that you probably didn't give them.

user-id-14900042
  • 686
  • 4
  • 17