java - String Replace() giving null pointer exception -


where iam going wrong?? no matter says null pointer exception on string.replace() want replace charecter file , write changed content other file.. though easy unable write it.help please!

package tinku; /*  * change license header, choose license headers in project properties.  * change template file, choose tools | templates  * , open template in editor.  */  import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.dataoutputstream; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.filereader; import java.io.filewriter; import java.io.ioexception; import java.io.inputstreamreader; import java.io.linenumberreader; import java.util.scanner;  /**  *  * @author gajasurve  */ public class tinku {     static int i;      /**      * @param args command line arguments      */     public static void main(string[] args) throws filenotfoundexception, ioexception {         // todo code application logic here         string str;          final string y;         string key;         fileoutputstream fos = new fileoutputstream("/home/gajasurve/desktop/done2.txt");         dataoutputstream output = new dataoutputstream(fos);         bufferedreader r = new bufferedreader(new inputstreamreader(system.in));         system.out.println("\n enter complete file path extension  (ex: /home/gajasurve/desktop/info.txt)  : ");         string source;         source=r.readline();         system.out.print("enter key element find in file ");         key=r.readline();            file f= new file(source);            linenumberreader lr= new linenumberreader(new filereader(f));          while((str=lr.readline())!=null)         {             if(str.contains(key))             {                 i=lr.getlinenumber();                 system.out.print("the entered key element can b found in " + + "   line");             }          }          system.out.println("do u wish change key element? y|n");         string dec= r.readline();         switch (dec) {             case "y" :                      system.out.print("enter new key element replace");                 string d2;                 d2 = r.readline();                 str= str.replaceall(key, d2);  //npe here                 system.out.println(str);                 break;         }     } } 

when run while loop, exit condition loop ends when str becomes null, hence when trying access str next time null , gives nullpointerexception.

you can fix running similar while loop around replaceall() code, or can move until replaceall() method call inside while loop. (note: ask if want replace key every occurrence)

possible fixed code:

package tinku;  /*  * change license header, choose license headers in project properties.  * change template file, choose tools | templates  * , open template in editor.  */  import java.io.*;  /**  *  * @author gajasurve  */ public class tinku {     static int i;      /**      * @param args command line arguments      */     public static void main(string[] args) throws filenotfoundexception, ioexception {         // todo code application logic here         string str;          final string y;         string key;         bufferedreader br = new bufferedreader(new inputstreamreader(system.in));          system.out.println("\n enter complete file path extension (ex: /home/gajasurve/desktop/info.txt)  : ");         string source;         source = br.readline();          file f= new file(source);         linenumberreader lr= new linenumberreader(new filereader(f));          system.out.print("enter key element find in file : ");         key = br.readline();          while((str = lr.readline()) != null) {             if(str.contains(key)) {                 = lr.getlinenumber();                 system.out.println("the entered key element can b found in " + + " line");             }          }          lr.close();          system.out.println("do u wish change key element? y|n");         string dec= br.readline();         switch(dec) {             case "y" : {                 system.out.print("enter string replace key : ");                 string d2;                 d2 = br.readline();                 lr= new linenumberreader(new filereader(f));                 string outputfile = "/home/gajasurve/desktop/done2.txt";                 printwriter pw = new printwriter(new filewriter(outputfile));                  while((str = lr.readline()) != null) {                     if(str.contains(key)) {                         pw.println(str.replaceall(key, d2));                     } else {                         pw.println(str);                     }                 }                 pw.close();                 break;             }         }     } } 

Comments