String.replace() 中的 java.lang.NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22362456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.NullPointerException in String.replace()
提问by mrahh
I feel like I should give an idea what the code is for. I have this problem where I need to write two functions, one is find() that returns a boolean value if it finds the index of a substring in a given string. The function is supposed to 'save the index values in the index array'. And another method, replace() that replaces all the searched words with the new word, and out put the new string. The index number of the matched words need to be output.
我觉得我应该知道代码的用途。我有这个问题,我需要编写两个函数,一个是 find(),如果它在给定字符串中找到子字符串的索引,则返回一个布尔值。该函数应该“将索引值保存在索引数组中”。另一种方法,replace() 用新词替换所有搜索到的词,并输出新的字符串。需要输出匹配词的索引号。
import java.util.Scanner;
public class findAndReplace {
public static void main(String[] args) {
String text;
String find;
String replace = null;
String result;
int index[];
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the line: ");
text = scanner.nextLine();
System.out.print("Enter the string to find: ");
find = scanner.nextLine();
System.out.print("Enter the string to replace with: ");
find = scanner.nextLine();
find(text, find);
String result1 = replace(text, find, replace); //!!NUllPointerException
System.out.print(result1);
}
public static boolean find(String text, String find){ //method return a Boolean value if it finds the index of the word that we are trying to find. Saves the index value in the index array.
boolean b = false;
int index_int;
int index[] = null;
if (text.indexOf(find)>=0){
b = true;
int i = 0;
do{
index_int = text.indexOf(find);
index[i]=index_int;
i++;
text.replace(text.charAt(index_int), '0');
System.out.print(index_int);
} while (text.indexOf(find)>=0);
}
return b;
}
public static String replace(String text, String find, String replace){ //method replaces all search words with new word and prints.
String result;
result = text.replace(find, replace); //!!NUllPointerException
return result;
}
}
Now the problem is, I am finding nullpointer exception at the commented lines. Can you tell me where the problem is? I searched the error to no avail.
现在的问题是,我在注释行中发现空指针异常。你能告诉我问题出在哪里吗?我搜索了错误无济于事。
回答by XSen
your replace is null; you assign to find twice.
您的替换为空;您分配查找两次。
回答by Raghvendra Singh
Your replace variable is null. You need to fix that
您的替换变量为空。你需要解决这个问题
回答by Mike B
Change this:
改变这个:
System.out.print("Enter the string to find: ");
find = scanner.nextLine();
System.out.print("Enter the string to replace with: ");
find = scanner.nextLine();
To this:
对此:
System.out.print("Enter the string to find: ");
find = scanner.nextLine();
System.out.print("Enter the string to replace with: ");
replace = scanner.nextLine();
回答by Arjit
Its because your replace variable is null, that you are passing as an argument.
因为您的替换变量为空,所以您将其作为参数传递。
As per Java docs
根据 Java 文档
" passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown"
回答by Hugo Kerstens
You made a little mistake, when you ask for the replace string, you store the input in the String find, and not in replace.
Replace it with this:
您犯了一个小错误,当您要求替换字符串时,您将输入存储在 String 中find,而不是replace. 用这个替换它:
System.out.print("Enter the string to replace with: ");
replace = scanner.nextLine();

