Tuesday, 15 January 2013

java - NullPointerException when storing lines in an arraylist -



java - NullPointerException when storing lines in an arraylist -

i'm getting nullpointerexception in java (at line :filereader fstream = new filereader(filename)) when seek read text file line line , store them in arraylist code :

public arraylist<string> storelinebyline(string filename) throws ioexception { string str; arraylist<string> line = new arraylist<string>(); filereader fstream = new filereader(filename); bufferedreader myfilereader = new bufferedreader(fstream); while ((str = myfilereader.readline()) != null) { line.add(str); } myfilereader.close(); homecoming line; }

could help me understand problem ? lot !

if you're getting npe on line, filename must null.

btw, if you're using jdk 8 may improve way load lines. replace contents of method this:

return files.readalllines(paths.get(filename), charset.defaultcharset());

...and alter method's homecoming type arraylist<string> list<string>.

java nullpointerexception

No comments:

Post a Comment