-3
// Documentation from interface
public SpannableStringBuilder append(CharSequence text) {
    int length = length();
    return replace(length, length, text, 0, text.length());
}

SpannableStringBuilder source code append() function doesn't avoid NullPointerException?? If text is null, append() function definitely throw NullPointerException. It's easily be forgotten to check every case if text is null. For example the text may from the server. Anyone has a better idea for SpannableStringBuilder append() function to avoid npe?

MLS
  • 617
  • 1
  • 7
  • 14
  • 2
    I'd say that the creators of this class left it up to the caller to make sure that a non `null` text is passed to the method. And, I don't see anything wrong with that decision. – Tim Biegeleisen Aug 16 '18 at 02:50
  • `SpannableStringBuilder#append(CharSequence text)` is not final - you can override it in some your custom class – pskink Aug 16 '18 at 04:32
  • It is up to you not to forget it. Or just extend this class, and override `append`, and use your extended class. – Vladyslav Matviienko Aug 16 '18 at 05:51

1 Answers1

0

You could wrap the SpannableStringBuilder in your own class and put the null check there .

GianhTran
  • 3,443
  • 2
  • 22
  • 42
Zachary Sweigart
  • 1,051
  • 9
  • 23