question

JassimAlRahma-9056 avatar image
0 Votes"
JassimAlRahma-9056 asked Viorel-1 commented

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

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@yahoo.com
Password : Bahrain1234

Kindly help..


Thanks,
Jassim


dotnet-csharpdotnet-xamarindotnet-standard
· 10
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

As the stack trace you provided says, the error occurs on line 205 of your code. Since you have only provided part of your code, no one will be able to look at it to determine what is null that should not be. A NullReferenceException is a very straightforward error and you will be able to find the issue by inserting a breakpoint and stepping through your code.

0 Votes 0 ·

The app crashes here:

 List<SigninData> data = JsonSerializer.Deserialize<List<SigninData>>(result);

and the class is:

 class SigninData
 {
     public string user_id { get; set; }
     public string user_uuid { get; set; }
     public string first_name { get; set; }
     public string last_name { get; set; }
     public string email_address { get; set; }
 }



0 Votes 0 ·

Did you inspect that line during execution to determine which part of it has a null value?

0 Votes 0 ·
Show more comments

Maybe result is empty or "[ ]". Did you check it?


0 Votes 0 ·

Try another email:

 . . .
 new KeyValuePair<string, string>("email", "jrahma@gmail.com"),
 new KeyValuePair<string, string>("password", "Bahrain1234")
 . . .


0 Votes 0 ·

I did that already. The problem is everything is fine with the URL POST on Postman and I am getting the correct result so I am 100% sure about the result but not sure why the code is throwing the error

0 Votes 0 ·
Viorel-1 avatar image Viorel-1 JassimAlRahma-9056 ·

It seems that you changed the code or data. Now “jrahma@yahoo.com” seems to work, but “jrahma@gmail.com” does not work anymore. Did you manage to get a valid result string before continuing the program?

You can also try simplifying signin.php to return a valid result regardless the email and password for testing purposes, in order to test the C# part. Maybe this .php file has some issues.


0 Votes 0 ·
Show more comments

1 Answer

JassimAlRahma-9056 avatar image
0 Votes"
JassimAlRahma-9056 answered

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

Thank you

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.