Skip to content
This repository has been archived by the owner on Apr 10, 2018. It is now read-only.

NullReferenceException with AddBody method #23

Closed
anatolykryzhanovsky opened this issue Mar 16, 2015 · 10 comments
Closed

NullReferenceException with AddBody method #23

anatolykryzhanovsky opened this issue Mar 16, 2015 · 10 comments

Comments

@anatolykryzhanovsky
Copy link

good day!
i try to use your library but have confused when need send json request to server:

var request = new RestRequest("/api/some_method", HttpMethod.Post);
request.AddBody(new MethodRequest
    {
        Parameter = "Parameter",
    });

var response = _client.Execute<MethodResponse>(request);
in runtime i have exception:
Object reference not set to an instance of an object. at
   at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)
   at Microsoft.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at Microsoft.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at RestSharp.Portable.RestClient.<Execute>d__7`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at App1.MainPage.<ButtonBase_OnClick>d__0.MoveNext()

other method (without request body) work fine
then i try to set up body with AddParameter:

request.AddParameter("text/html", new MethodRequest
    {
        Parameter= "Parameter",
    }, ParameterType.RequestBody);

and this work perfectly

fubar-coder added a commit that referenced this issue Mar 16, 2015
@fubar-coder
Copy link
Contributor

I created a small unit test, but I cannot reproduce the error. Maybe you can provide a small test case?

@anatolykryzhanovsky
Copy link
Author

ok, i create simple app - one button, one method, problem reproduce on my computer:
Response object

class Response
{
    public string Code { get; set; }
}

Request object

class Request
{
    public string Parameter { get; set; }
}

Service wrapper

class Service
{
    private readonly RestClient _client;

    public Service()
    {
        _client = new RestClient("http://localhost");
    }

    public Task<IRestResponse<Response>> TestMethod()
    {
        var request = new RestRequest("/method", HttpMethod.Post);
        request.AddBody(new Request
            {
                Parameter = "parameter"
            });

        var response = _client.Execute<Response>(request);

        return response;
    }
}

button handler - initiate request

private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
    var service = new Service.Service();
    var result = await service.TestMethod(); // exception there

   Debug.WriteLine(result.Data.Code);
}

test solution - https://dl.dropboxusercontent.com/u/75125692/WPTest.7z

@fubar-coder
Copy link
Contributor

I just took a look at the sample application, and the only place where I get a NullReferenceExceptionis the line Debug.WriteLine(result.Data.Code); when the returned content type is unrecognized and the Data member is NULL. I tested it both in an emulator and with a hardware device (Lumia 720).

@anatolykryzhanovsky
Copy link
Author

hm, thats strange, becouse i have exception on await.. ok i'll test on another pc

@fubar-coder
Copy link
Contributor

Closed, because the problem is not reproducible.

@piotrek1231
Copy link

I have the same problem. When my API return string all is ok, but if I want to obtain object from json, still get this error.

@fubar-coder
Copy link
Contributor

Can you provide a test case and a stack trace, because I'm unable to reproduce the error.

@fubar-coder
Copy link
Contributor

Closed, because I cannot reproduce this problem.

@GeirGrusom
Copy link

It may be caused by this line.

So it happens if you have a HttpBasicAuthenticator on the client. A workaround is to call client.Authenticator.Authenticate before AddBody, since there at that point will be no parameters without a name (which is what causes the NullReferenceException).

You should be able to reproduce it with this:

var client = new RestClient("http://www.google.com");
client.Authenticator = new HttpBasicAuthenticator("foo", "bar");
var request = new RestRequest("Foo").AddBody("foo");
await client.Execute(request);

It will fail on the Execute.

@fubar-coder
Copy link
Contributor

Thank you, I'll update it soon,

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants