Why String concatenate null with + operator and throws NullPointerException with concate() method

后端 未结 9 1791
别那么骄傲
别那么骄傲 2021-02-04 06:23

Here is my class, where i am concatenating two string. String concatenate with null using + operator execute smoothly but throws NullPointerException

9条回答
  •  猫巷女王i
    2021-02-04 06:36

    Actual implementation of concate method is like this . this method need an object of type String as a parameter and if argument is null then it throws Null Pointer Exp.

    public String concat(String paramString)
      {
        int i = paramString.length();
        if (i == 0) {
          return this;
        }
        int j = this.value.length;
        char[] arrayOfChar = Arrays.copyOf(this.value, j + i);
        paramString.getChars(arrayOfChar, j);
        return new String(arrayOfChar, true);
      }
    

提交回复
热议问题