java - Nullpointer Exception reading in file line by line -
txt file reading program line line. goal display text no punctuations , in lowercase. far able display text in way code below keep receiving run-time error of nullpointer exception @ last line of "while" expression , not sure error is. believe problem may have been in boolean while expression , tried changing input != null
reader.readline() != null
no success. thank you!
import java.io.filereader; import java.io.bufferedreader; import java.io.filenotfoundexception; import java.io.ioexception; public class binarytree { public static void main(string[] args) { string inputfile = "pg345.txt"; filereader filereader = null; try { filereader = new filereader(inputfile); } catch (filenotfoundexception e) { e.printstacktrace(); } bufferedreader reader = new bufferedreader(filereader); string input; try { input = reader.readline().tolowercase().replaceall("[^a-za-z ]", "").tolowercase(); system.out.println(input); while (input != null) { input = reader.readline().tolowercase().replaceall("[^a-za-z ]", "").tolowercase(); system.out.println(input); } } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } try { filereader.close(); } catch (ioexception e) { e.printstacktrace(); } } }
your culprit is
reader.readline().tolowercase().replaceall("[^a-za-z ]", "").tolowercase() reader.readline().tolowercase().replaceall("[^a-za-z ]", "").tolowercase();
here invocation
reader.readline()
might return null if there no more lines in file.
Comments
Post a Comment