笔者今天在使用fastjson序列化某个对象(传其他应用传回的数据)时出现了上面的错误提示:
代码语言:javascript复制write javaBean error, fastjson version 1.2.47, class
xxxx
xxxx
Caused by: java.lang.NullPointerException: null
最终发现是该类的某个get方法出现了空指针异常导致了上面的问题,这个就涉及到String类的contentEquals与equals方法的不同了,具体
代码语言:javascript复制 public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i ;
}
return true;
}
}
return false;
}
public boolean contentEquals(CharSequence cs) {
// Argument is a StringBuffer, StringBuilder
if (cs instanceof AbstractStringBuilder) {
if (cs instanceof StringBuffer) {
synchronized(cs) {
return nonSyncContentEquals((AbstractStringBuilder)cs);
}
} else {
return nonSyncContentEquals((AbstractStringBuilder)cs);
}
}
// Argument is a String
if (cs instanceof String) {
return equals(cs);
}
// Argument is a generic CharSequence
char v1[] = value;
int n = v1.length;
if (n != cs.length()) {
return false;
}
for (int i = 0; i < n; i ) {
if (v1[i] != cs.charAt(i)) {
return false;
}
}
return true;
}
从上面代码可以看出,contentEquals并没有对传入参数进行null值校验,不过笔者这个存储笔误(Eclipse的自动补齐!!)