1
Vote

when post data, console show "System.NullReferenceException"

description

httplib 2.0.12

When I post login data to server, the httplib everytime write “System.NullReferenceException” in console.

Http.Post(httpUrl).Form(new { admin="admin", password="123456" }).OnSuccess(response =>
                {
                    syncContext.Post(callback, response);
                }).Go();

“System.NullReferenceException”类型的第一次机会异常在 JumpKick.HttpLib.dll 中发生
“System.NullReferenceException”类型的第一次机会异常在 JumpKick.HttpLib.dll 中发生

comments

JumpKick wrote Dec 26, 2015 at 12:32 PM

This information ins't enough to reproduce the error. Is syncContext your object?

Are you checking that response is not null?

taowenyin wrote Dec 28, 2015 at 2:53 AM

Value of response is not null. syncContext's type is SynchronizationContext, it used to post data to UI thread.

The softerware can normal running. Because ervery time post data to server, HttpLib will write “System.NullReferenceException” in console, so I'm worried about the software stability.
class HttpUtility
{
    public static void HttpSyncOperate(SynchronizationContext syncContext, SendOrPostCallback callback, 
        int method, string httpUrl, Dictionary<string, string> httpParams = null)
    {
        string serialParams = "";

        if(httpParams != null)
        {
            serialParams = HttpParams2Serial(httpParams);
        }

        switch (method)
        {
            case MethodConfig.HttpMethod.HTTP_GET:
                Http.Get(httpUrl + "?" + serialParams).OnSuccess(response =>
                {
                    syncContext.Post(callback, response);
                }).Go();
                break;

            case MethodConfig.HttpMethod.HTTP_POST:
                Http.Post(httpUrl).Form(httpParams).OnSuccess(response =>
                {
                    syncContext.Post(callback, response);
                }).Go();
                break;
        }
    }

    private static string HttpParams2Serial(Dictionary<string, string> httpParams)
    {
        string serialParams = "";

        foreach(KeyValuePair<string, string> param in httpParams)
        {
            string key = param.Key;
            string value = param.Value;

            serialParams += (key + "=" + value + "&");
        }

        // 把最后一个&剪掉
        serialParams = serialParams.Remove(serialParams.Length - 1);

        return serialParams;
    }
}