It looks to me that each time you go through the loop, you assign the charCount to be the length of the line that iteration of the loop is concerned with. i.e. instead of
charCount = s.Length() -1;
try
charCount = charCount + s.Length();
EDIT:
If you have say the document with the contents "onlyOneLine
"
Then when you first hit the while
check the br.readLine()
will make the BufferredReader
read the first line, during the while
's code block however br.readLine()
is called again which advances the BufferredReader
to the second line of the document, which will return null
. As null
is assigned to s
, and you call length()
, then NPE is thrown.
try this for the while block
while ( (c=br.readLine()) != null) {
charCount = charCount + c.Length(); }