我哪里错了?无所谓我做什么它说String.replace()上的空指针异常 我想从文件中替换Charecter并将更改的内容写入其他文件..即使它很容易我也无法写它。请帮助!
package tinku;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the 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 the 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 with Extension (ex: /home/gajasurve/Desktop/info.txt) : ");
String Source;
Source=r.readLine();
System.out.print("Enter Key Element to Find In the 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 " + i + " Line");
}
}
System.out.println("Do u wish to change the Key Element? Y|N");
String Dec= r.readLine();
switch (Dec) {
case "Y" :
System.out.print("Enter New Key Element to Replace");
String d2;
d2 = r.readLine();
str= str.replaceAll(Key, d2); //NPE here
System.out.println(str);
break;
}
}
}
答案 0 :(得分:3)
当您运行while循环时,退出条件是当str
变为null
时循环结束,因此当您下次尝试访问str
时它为空并且所以给出一个NullPointerException
。
您可以通过在while
代码周围运行类似的replaceAll()
循环来解决此问题,或者您可以移动所有内容,直到replaceAll()
循环内的while
方法调用。 (注意:这将询问您是否要在每次出现时替换Key
可能的固定代码:
package tinku;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
/**
*
* @author gajasurve
*/
public class Tinku {
static int i;
/**
* @param args the 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 with 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 to Find In the File : ");
Key = br.readLine();
while((str = lr.readLine()) != null) {
if(str.contains(Key)) {
i = lr.getLineNumber();
System.out.println("The Entered Key Element Can B Found In " + i + " Line");
}
}
lr.close();
System.out.println("Do u wish to change the Key Element? Y|N");
String Dec= br.readLine();
switch(Dec) {
case "Y" : {
System.out.print("Enter String to replace Key with : ");
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;
}
}
}
}