一開始認為在構造json相關的對象時,如果傳輸的字元串參數為null,也會當作JSONException抛出,但是今天遇到了發現不是,會抛出一個NullPointerException,這裡需要我們進行一下相關的處理。
如下列代碼:
public void testJSONConstructor() {
final String jsonStr = null;
try {
JSONObject jsonObj = new JSONObject(jsonStr);
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
然而異常被捕獲為NullPointerException,而不是JSONObjectException,但是為什麼呢,明明如下代碼聲明
public JSONObject(String json) throws JSONException {
this(new JSONTokener(json));
}
對,上面的代碼确實是标明了扔出JSONException,那我們看看是如何抛出這個NPE的
首先我們看JSONTokener的構造函數,帶字元串參數的這個
public JSONTokener(String in) {
// consume an optional byte order mark (BOM) if it exists
if (in != null && in.startsWith("\ufeff")) {
in = in.substring(1);
}
this.in = in;
}
而且this.in的聲明是這樣的
/** The input JSON. */
private final String in;
是以如何參數in為null,則成員屬性也為null
下面是JSONObject帶JSONTokener的構造函數
public JSONObject(JSONTokener readFrom) throws JSONException {
/*
* Getting the parser to populate this could get tricky. Instead, just
* parse to temporary JSONObject and then steal the data from that.
*/
Object object = readFrom.nextValue();
if (object instanceof JSONObject) {
this.nameValuePairs = ((JSONObject) object).nameValuePairs;
} else {
throw JSON.typeMismatch(object, "JSONObject");
}
}
那麼是哪裡引起的NPE呢,JSONTokener調用的nextValue方法
public Object nextValue() throws JSONException {
int c = nextCleanInternal();
switch (c) {
case -1:
throw syntaxError("End of input");
case '{':
return readObject();
case '[':
return readArray();
case '\'':
case '"':
return nextString((char) c);
default:
pos--;
return readLiteral();
}
}
nextValue又調用的nextCleanInternal
private int nextCleanInternal() throws JSONException {
while (pos < in.length()) {
int c = in.charAt(pos++);
switch (c) {
case '\t':
case ' ':
case '\n':
case '\r':
continue;
//由于方法體過長,省略一下
是以,我們看到了之前的成員屬性in 由于參數為null,沒有被指派,是以還是預設的Null,是以這裡就出現了NullPointerException