3

I do not understand why when I try to log in, the program returns a System.NullReferenceException, when I perform a check to prevent the values entered from being null

Here's the model:

using Newtonsoft.Json;

namespace StockingApp.Models
{
public class LoginModel
{
    [JsonProperty("email")]
    public string Email { get; set; }

    [JsonProperty("password")]
    public string Password { get; set; }
  }
}

Here's the code I use to sign in:

private async void loginButton_Clicked(object sender, EventArgs e)
    {
        LoginModel log = new LoginModel
        {
            Email = txtEmail.Text,
            Password = txtPassword.Text
        };

        if (txtEmail.Text != null | txtPassword.Text != null)
        {
            Uri RequestUri = new Uri("The url of the Api");

            var client = new HttpClient();
            var json = JsonConvert.SerializeObject(log);
            var contentJson = new StringContent(json, Encoding.UTF8, "application/json");
            //this is the line that causes the exception
            var response = await client.PostAsync(RequestUri, contentJson);

            if (response.IsSuccessStatusCode)
            {
                await DisplayAlert("Iniciar Sesión", "El inicio de sesión se realizó correctamente", "Ok");
            }
            else
            {
                await DisplayAlert("Error", "Los datos introducidos son incorrectos", "Ok");
            }
        }
        else
        {
            await DisplayAlert("Error", "Faltan datos por introducir", "Ok");
        }
    }

This is the line that cause the exception, i don't understand why response is null

var response = await client.PostAsync(RequestUri, contentJson);

Here's the Xaml & How the Xaml looks: How it looks

<ContentPage.Content>
    <StackLayout Spacing="5" Padding="10" VerticalOptions="Center">
    <Entry x:Name="txtEmail" Placeholder="{translator:Translate prompt_email}" Style="{StaticResource entryStyles}"/>
    <Entry x:Name="txtPassword" Placeholder="{translator:Translate prompt_password}" Style="{StaticResource entryStyles}" IsPassword="True"/>
    <Button x:Name="loginButton" Text="{translator:Translate title_activity_login}" Style="{StaticResource buttonStyles}" Clicked="loginButton_Clicked"/>
    <Label  Text="{translator:Translate tit_contacto}" HorizontalTextAlignment="Center"/>
    <Label x:Name="link" Margin="20" HorizontalOptions="Center">
        <Label.FormattedText>
            <FormattedString>
                <Span Text="www.atecresa.com"  TextColor="GreenYellow" TextDecorations="Underline">
                    <Span.GestureRecognizers>
                        <TapGestureRecognizer Command="{Binding TapCommand}"
                                      CommandParameter="https://atecresa.com" NumberOfTapsRequired="1"/>
                    </Span.GestureRecognizers>
                </Span>
            </FormattedString>
        </Label.FormattedText>
    </Label>
</StackLayout>
</ContentPage.Content>

This is the swagger to log in: Swagger of the api

I did a check on Postman and the api works properly: Postman check

6
  • please do NOT post code or errors as images. Take the time to post the relevant code, properly formatted, in the body of your post Commented May 18, 2022 at 11:57
  • @Jason Okay I change that Commented May 18, 2022 at 12:43
  • Great! Now, which specific line is causing the exception? The stack trace should show your the line number, or you can step through the code in the debugger to find it. Commented May 18, 2022 at 12:55
  • @Jason I just edited the question to specify the line that causes the exception Commented May 18, 2022 at 14:07
  • 1
    it doesn't appear that anything on that line could be null - what does the stack trace show? Is the actual exception occurring down inside that method? Commented May 18, 2022 at 14:12

2 Answers 2

2

I have a similar error, i just change the http of the url to https. I think thats the problem here.

Sign up to request clarification or add additional context in comments.

Comments

1

Maybe try this

if (txtEmail.Text != null && txtPassword.Text != null)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.