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

后端 未结 9 1770
别那么骄傲
别那么骄傲 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条回答
  •  别跟我提以往
    2021-02-04 06:32

    If you see in java.lang.String source, and use null as parameter. NPE is thrown in first line in length() method.

    public String concat(String str) {
        int otherLen = str.length();//This is where NullPointerException is thrown
        if (otherLen == 0) {
            return this;
        }
        getChars(0, count, buf, 0);
        str.getChars(0, otherLen, buf, count);
        return new String(0, count + otherLen, buf);
    }
    

提交回复
热议问题