杰克逊无法解析json,返回NPE

时间:2015-05-03 17:18:58

标签: java json parsing jackson

我正在尝试解析看起来像这样的json(steam webchat)(我已经改变了响应因为我不想显示数据):

/**/({
"pollid": 00,
"messages": [
    {
        "type": "personastate",
        "timestamp": 0000000000,
        "utc_timestamp": 000000000,
        "steamid_from": "000000000000",
        "status_flags": 0000000,
        "persona_state": 0,
        "persona_name": "asd"
    }
]
,
"messagelast": 00,
"timestamp": 0000000000,
"utc_timestamp": 000000000000,
"messagebase": 00,
"sectimeout": 0,
"error": "OK"
})

我的解析类看起来像这样:

package jsonRequest;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

public class NewMessageJson {

public Integer poollid;
private String lastMessageId;
private String error;
private String messageBase;

public NewMessageJson(String response) {
response = response.substring(response.indexOf("{"),
    response.indexOf("}") + 1); // cut off comment block


JsonFactory factory = new JsonFactory();
JsonParser jp = null;
try {
    jp = factory.createJsonParser(response);
} catch (JsonParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
try {

    if (jp.nextToken() != JsonToken.START_OBJECT) {
    throw new IOException("Server didn't return any data");
    }

    while (jp.nextToken() != JsonToken.END_OBJECT) {
    String fieldName = jp.getCurrentName();
    jp.nextToken();
    if (fieldName.equals("messagelast")) {
        setLastMessageId(jp.getText());
    } else if (fieldName.equals("pollid")) {
        setPoollid(jp.getIntValue());
    } else if (fieldName.equals("messagebase")) {
        setMessageBase(jp.getText());
    } else if (fieldName.equals("error")) {
        setError(jp.getText());
    }
    }
    jp.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (NullPointerException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

try {
    jp.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}

public Integer getPoollid() {
return poollid;
}

public void setPoollid(Integer poollid) {
this.poollid = poollid;
}

public String getLastMessageId() {
return lastMessageId;
}

public void setLastMessageId(String lastMessageId) {
this.lastMessageId = lastMessageId;
}

public String getError() {
return error;
}

public void setError(String error) {
this.error = error;
}

public String getMessageBase() {
return messageBase;
}

public void setMessageBase(String messageBase) {
this.messageBase = messageBase;
}

}

当谈到这条线

if (fieldName.equals("messagelast")) {

它崩溃并返回NPE。 我还有其他3个类看起来就像这个类,一切都很完美。

1 个答案:

答案 0 :(得分:0)

我很确定您获得NPE的原因是因为您最初将JsonParser jp实例化为null。您将其分配给factory.createJsonParser(response)块中的try,但除了打印堆栈跟踪之外,不以任何方式处理错误。如果执行factory.createJsonParser(response)时出错,则需要确保没有其他任何操作。

我建议您将代码更改为:

...

JsonFactory factory = new JsonFactory();
JsonParser jp = null;
try {
    jp = factory.createJsonParser(response);
} catch (JsonParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    System.out.println("There was an error while setting jp to factory.createJsonParser(response). Error message is: " + e1.getMessage());
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    System.out.println("There was an error while setting jp to factory.createJsonParser(response). Error message is: " + e1.getMessage());
}

if(jp != null) {
try {

    if (jp.nextToken() != JsonToken.START_OBJECT) {
    throw new IOException("Server didn't return any data");
    }

    while (jp.nextToken() != JsonToken.END_OBJECT) {
    String fieldName = jp.getCurrentName();
    jp.nextToken();
    if (fieldName.equals("messagelast")) {
        setLastMessageId(jp.getText());
    } else if (fieldName.equals("pollid")) {
        setPoollid(jp.getIntValue());
    } else if (fieldName.equals("messagebase")) {
        setMessageBase(jp.getText());
    } else if (fieldName.equals("error")) {
        setError(jp.getText());
    }
    }
    jp.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (NullPointerException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

try {
    jp.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}
}

...

这样,您可以避免所有NPE!

编辑:您还应该实施peeskillet建议的内容