Volley报NullPointerException

Volley报NullPointerException原因

报错日志如下:

03-11 12:46:09.681 16811-16836/com.ushopal.batman E/Volley: [11762] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException

                                                                        java.lang.NullPointerException
                                                                            at libcore.net.UriCodec.encode(UriCodec.java:132)
                                                                            at java.net.URLEncoder.encode(URLEncoder.java:57)
                                                                            at com.android.volley.Request.encodeParameters(Request.java:463)
                                                                            at com.android.volley.Request.getBody(Request.java:449)
                                                                            at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:236)
                                                                            at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:210)
                                                                            at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:106)
                                                                            at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
                                                                            at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)

其实看log的堆栈就知道,volley.Request.getBody一直调用到UriCodec.encode,报了这个错。我们查看这些方法的源代码:

 public byte[] getBody() throws AuthFailureError {
        Map params = getParams();//Volley Request提交的params参数
        if (params != null && params.size() > 0) {
            return encodeParameters(params, getParamsEncoding());
        }
        return null;
    }

private byte[] encodeParameters(Map params, String paramsEncoding) {
    StringBuilder encodedParams = new StringBuilder();
    try {
        for (Map.Entry entry : params.entrySet()) {
            encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
            encodedParams.append('=');
            encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
            encodedParams.append('&');
        }
        return encodedParams.toString().getBytes(paramsEncoding);
    } catch (UnsupportedEncodingException uee) {
        throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
    }
}
------URLEncoder.java--------------------
public static String encode(String s, String charsetName) throws UnsupportedEncodingException {
    return ENCODER.encode(s, Charset.forName(charsetName));
}

既然是encode报空,很有可能是s为空。这个s是encodeParameters中的entry.getKey()或者entry.getValue();
所以检查你在使用valley请求的时候传递的参数,不管是key或value为空,都会导致这个exception。而且这个exception被捕获,最终通过Response.ErrorListener回调。所以如果你没在回调接口中做相关处理,也很难发现这个问题。

你可能感兴趣的:(Android)