1

My code is as follows:

import java.util.*;  
import java.io.*;  
public class Buffer
{
    public static void main(String args[]) throws Exception
    {            
        int T;
        Scanner sc = new Scanner(System.in);
        T = sc.nextInt();
        while(T > 0)
        {
            InputStreamReader r = new InputStreamReader(System.in);    
            BufferedReader br = new BufferedReader(r);           
            String no = "";

            no = br.readLine(); 
            char ch = no.charAt(0);

            if (ch == '0')
            {
                System.out.println("YES");
            }
            else
            {
                System.out.println("NO");
            }
            T--;
        }              
    }    
}    

Please help me with my code, what is wrong exactly?

I am getting the output:

Run Time Error Exception in thread "main"<br>

java.lang.NullPointerException at Buffer.main

saurabh
  • 45
  • 1
  • 1
  • 5
  • 2
    1. Which is line 19? 2. NPEs are extremely common errors, and are explained in detail in many guides. Have you looked up what this error means? – Carcigenicate Sep 27 '17 at 11:25
  • How about `if (no.matches("0.*"))` instead of so much work you have done? – Tim Biegeleisen Sep 27 '17 at 11:29
  • Also don't open the stream in the loop – John Hascall Sep 27 '17 at 11:30
  • In short a `NullpointerException` means that you are trying to call something, like a **method** on an object that is currently pointing to `null` instead of a *valid object*. Looking at your code it could be this line `char ch = no.charAt(0);` where the object `no` is currently `null` instead of a `String` holding the current line. Take a look at the documentation of [`BufferedReader#readLine`](https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html#readLine()) which says it returns `null` if the end of the stream was reached. That is probably the source of the problem. – Zabuzard Sep 27 '17 at 11:37
  • Many things wrong with your code. Why for example are you *first* creating a Scanner, to then using readers to read from stdin? That is nonsense - go with the scanner alone instead. Seriously: you are doing trial/error programming. That doesn't work when you have **no** idea what you are doing. Follow tutorials, top to bottom instead of putting up code yourself that sorry ... doesn't make any sense. – GhostCat Sep 27 '17 at 11:37

1 Answers1

0

I think the problem is that you used two readers (Scanner and BufferedReader) in the same time, in fact it's more than two what you used: each time you enter the loop you instantiate a BufferedReader trying to read the System.in.

Are you trying a competitive programming problem ? if so, I suggest that you stick with the BufferedReader:

import java.util.*;
import java.io.*;
public class Buffer {

public static void main(String args[])throws Exception {

    int T;
    InputStreamReader r = new InputStreamReader(System.in);    
    BufferedReader br = new BufferedReader(r);
    T = Integer.parseInt(br.readLine());
    while(T>0)
    {      
      String no=br.readLine(); 
      char ch=no.charAt(0);

      if(ch=='0')
      {
       System.out.println("YES");
      }
      else
      {
        System.out.println("NO");
      }
      T--;
    }
  }
}
0Be95
  • 36
  • 4