java - NullPointerException reading from text -


public static void wordcounter(string target,bufferedreader source) throws ioexception {         hashmap<string, integer> map = new hashmap<string, integer>();          while(source.readline() != null ) {             string row = source.readline();             string[] separated = row.split(" ");             (int i=0; i< separated.length;i++) {                 separated[i] = separated[i].replaceall("=+-!?'\".,:;", "");             }              (int i=0; i< separated.length;i++) {                 if ( map.containskey(separated[i]) ) {                     int k = (integer) map.get(separated[i]);                     map.put(separated[i], (k+1));                 }                 else {                     map.put(separated[i], 1);                 }             }         }          if (map.containskey(target)) {             system.out.println( "target word:" + target +                     "\nappears: " + map.get(otsitavsona) + " times." );         }         else {             system.out.println( "target word not found in source." );         }     } 

this method created read source , map different words , return number of occurances of specified word. problem @ line string[] separated = row.split(" "); nullpointerexception. causes , how fix problem?

thanks.

in original code, first check in while statement pass second time source.readline() called, have hit end of stream (consider situation when there 1 more line read , after while check enters loop). per documentation bufferedreader.readline(), returns null when end of stream reached, , should reason nullpointerexception.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -