0

My application Android display this error : "Object reference not set an instance of an object" when I execute my code. It's as if there was nothing in "e.Result". However, My webservice works well

 public MainPage()
        {
            InitializeComponent();


            Please.GardaSoapClient Ws = new Please.GardaSoapClient(new BasicHttpBinding(),
                        new EndpointAddress("http://webgarda20170508110006.azurewebsites.net/Garda.asmx"));
            Ws.HelloCompleted += Ws_HelloCompleted;
            Ws.HelloAsync();

            }

        private void Ws_HelloCompleted(object sender, Please.HelloCompletedEventArgs e)
        {
            Device.BeginInvokeOnMainThread(async () => {
                    string error = null;
                     if (e.Error != null)
                         error = e.Error.Message;
                     else if (e.Cancelled)
                         error = "Cancelled";

                     if (!string.IsNullOrEmpty(error))
                     {
                         await DisplayAlert("Error", error, "OK", "Cancel"); **//error => "Object reference not set an instance of an object"**
                     }
                     else 
             `enter code here`       {

                test.Text = e.Result;
               }
            });
        }
    } 

Can you help me ? :) Thanks

  • I haven't investigate this, but you can always do the http request manually. Have a look at this: http://stackoverflow.com/a/4015346/1845593 – user1845593 May 16 '17 at 10:35
  • 1
    Another thing you can do, is to send the request over Fiddler and check the errors that are coming from the server or if you are hitting the server – user1845593 May 16 '17 at 10:37

1 Answers1

0

As the target method is an event handler, you can Take advantage of the async event handler to keep the event arg e in scope.

private async void Ws_HelloCompleted(object sender, Please.HelloCompletedEventArgs e) {
    string error = null;
    if (e.Error != null)
        error = e.Error.Message;
    else if (e.Cancelled)
        error = "Cancelled";

    if (!string.IsNullOrEmpty(error)) {
        await DisplayAlert("Error", error, "OK", "Cancel");
    } else {
        test.Text = e.Result;
    }
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472