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

后端 未结 9 1773
别那么骄傲
别那么骄傲 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:47

    The String class holds an array (probably an ArrayList) of characters. When you call .concat() it goes through and adds every character from the second string to the first.

    If the first String is null, there is nothing to add to, causing a NullPointer Exception. Try initializing Strings with "".

提交回复
热议问题