System.NullReferenceException: Object reference not set to an instance of an object

Jassim Al Rahma 1,616 Reputation points
2021-06-07T19:55:27.133+00:00

Hi,

I am trying the below code but it keeps throwing error when I use it on my app. Although I am not getting any error when using the URL in Postman and everything is fine there

Here is the code:

var client = new HttpClient();  
  
client.BaseAddress = new Uri("https://www.jassimrahma.net/temp/signin.php");  
  
var content = new FormUrlEncodedContent(new[]  
{  
    new KeyValuePair<string, string>("email", TextBoxSigninEmailAddress.Text.Trim()),  
    new KeyValuePair<string, string>("password", TextBoxSigninPassword.Text.Trim())  
});  
  
var response = await client.PostAsync("https://www.jassimrahma.net/temp/signin.php", content);  
  
var result = await response.Content.ReadAsStringAsync();  
  
List<SigninData> data = JsonSerializer.Deserialize<List<SigninData>>(result);  
  
if (data.Count <= 0)  
{  
    await App.Current.MainPage.DisplayAlert("Error", "Unable to find your account!", "Try Again");  
  
    TextBoxSigninPassword.Text = "";  
  
    BusyIndicatorSignin.IsBusy = false;  
  
    ButtonForgotPassword.IsVisible = true;  
    ButtonSigninNow.IsVisible = true;  
  
    return;  
}  
else  
{  
    await SecureStorage.SetAsync("UserID", data[0].user_id);  
  
    popupLayout.IsOpen = false;  
}  

Here is the error I am getting:

System.NullReferenceException: Object reference not set to an instance of an object
at Zeera.Signin.ButtonSigninNow_Clicked (System.Object sender, System.EventArgs e) [0x00416] in /Users/jassim/Projects/Zeera/Zeera/Signin.xaml.cs:205
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021
at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSAction.cs:178
at at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:86
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0000e] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:65
at Zeera.iOS.Application.Main (System.String[] args) [0x00001] in /Users/jassim/Projects/Zeera/Zeera.iOS/Main.cs:17

you can try with the following:

Email: jrahma@réalisations .com
Password : Bahrain1234

Kindly help..

Thanks,
Jassim

Developer technologies .NET Xamarin
Developer technologies .NET Other
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Jassim Al Rahma 1,616 Reputation points
    2021-06-07T22:42:25.91+00:00

    Problem solved. I deleted bin and obj folders, closed VS for Mac and rebuilt the app again.

    Thank you

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.