-2
for(File d : documents)
    {

        if(d.isFile())
        count++;
        {
            BufferedReader inputStream = null;
            try 
            {
                inputStream = new BufferedReader(new FileReader(d));
                String line;
                while ((line = inputStream.readLine()) !=null)
                { 
                       //condition to check the hyphen at end of line                       
                       if(line.charAt(line.length() -1) == 45)
                       {
                          line = line.replace(line.charAt(line.length() -1),' ');                                   
                          String line2 = inputStream.readLine();
                          line = line.trim()+line2;
                       }
                    System.out.println(line);

            }
            finally
            {
                try
                {
                    if(inputStream != null)
                    inputStream.close();
                }
                catch(IOException e)
                {
                }
            }
        }
       // System.out.println("\n" +  tokens1);
        //System.out.println("\n" +  count);
    }
}
catch(Exception e)
{
    System.out.println("Null point exception");
}

When I remove the condition to check hyphen, it reads all the lines in the files and displays null pointer exception at the end. When I include this condition, it reads the file but whenever it finds first empty line, it stops and throws a null pointer exception.

Krusty the Clown
  • 517
  • 6
  • 24

1 Answers1

0

When you try to readLine inside if , You havent made any null checking that's why it results in NPE

  while ((line = inputStream.readLine()) !=null)
            { 
                   //condition to check the hyphen at end of line                       
                   if(line.charAt(line.length() -1) == 45)
                   {
                      line = line.replace(line.charAt(line.length() -1),' ');                                   
                      String line2 = inputStream.readLine(); // No null checking is being made here
                      line = line.trim()+line2;
                   }
                System.out.println(line);

        }
Mayur Kulkarni
  • 1,306
  • 10
  • 28