0
class WordCount
{
    String resource;
    void read () throws IOException
    {
         File file = new File("C:\\Users\\Desktop\\read.txt");  
         BufferedReader br = new BufferedReader(new FileReader(file)); 
         while ((resource = br.readLine()) != null)
         {
                 resource.trim();
         }
         System.out.println(resource.length());
         br.close();
    }
}
Kevin Anderson
  • 4,388
  • 2
  • 10
  • 20
Naveen
  • 1
  • 1
    We know from the loop condition that `resource` will always be non-null inside the loop, so `resouce.trim()`can't be the error. The same loop condition also guarantees that`resource==null` when the loop is finished. And what's right after the loop? Yep, `System.out.println(resource.length())`. KABOOM! Did you mean to put that `println` _inside_ the loop? – Kevin Anderson Sep 24 '19 at 10:57
  • Your file always have a data in your code. your resource variable data is changing. what are actually trying to achieve using System.out.println(resource.length()); – madhepurian Sep 24 '19 at 11:24

1 Answers1

1

Here you are assigning null at final step of loop:

while ((resource = br.readLine()) != null)
madhepurian
  • 271
  • 1
  • 13