Java replaceAll抛出空指针异常

Jay*_*esh 1 java

在使用String类的replaceAll API时,我遇到了Null Pointer Exception.

然后我尝试用下面粘贴的小片段,我正在接受NPE.

    String s = "HELLO @WORLD@ I AM.";
    System.out.println(s.replaceAll("@WORLD@", null)) ; 
Run Code Online (Sandbox Code Playgroud)

我也发现在String.java类中

  public Matcher appendReplacement(StringBuffer sb, String replacement) {
        .......//code

        while (cursor < replacement.length()) { ..//code}
        .......//code
}
Run Code Online (Sandbox Code Playgroud)

所以这里它称之为replacement.length()NPE的原因.

是否有规则我们不能将第二个参数作为null传递?

我知道如果替换单词为null,JVM将替换什么.

Pan*_*rma 5

用空字符串替换字符串而不是null. s.replaceAll("@WORLD@", "" )