I'm pretty new to xamarin and have gotten an issue where I'm getting a null reference when I do a post request to my backend. I've fully ran through the code multiple times and every thing works as intended, the post gets sent and is properly handeld by the backend and a ok() is sent back to the front end. But every time I continue any code it instantly gives me a null reference exception and i'm unsure what my stacktrace is trying to tell me.
If anyone would be able to help or point me in the right direction that be great, thank you for your time and help.
This is my code i use in my viewmodel to get the data from my xaml and send it to the post methode
namespace student_app.ViewModels
{
public class AddCategoryViewModel : BaseViewModel
{
private readonly IToastService _toastService;
private readonly INavigationService _navigationService;
private readonly ICategoryService _categoryService;
public string categoryName;
public Command SaveCommand { get; }
public Command LoadCategoriesCommand { get; }
public event PropertyChangingEventHandler PropertyChanged;
public string CategoryNameEditor
{
get => categoryName;
set
{
categoryName = value;
var args = new PropertyChangingEventArgs(nameof(CategoryNameEditor));
PropertyChanged?.Invoke(this, args);
}
}
public AddCategoryViewModel(IToastService toastService, INavigationService navigationService, ICategoryService categoryService)
{
_toastService = toastService;
_navigationService = navigationService;
_categoryService = categoryService;
SaveCommand = new Command(async () => await ExecuteSaveCommandCommand());
LoadCategoriesCommand = new Command(async () => await ExecuteLoadCategoriesCommand());
}
public async Task ExecuteLoadCategoriesCommand()
{
IsBusy = true;
try
{
//CategoryNameEditor = string.Empty;
}
catch (Exception ex)
{
await _toastService.DisplayToastAsync(ex.Message);
}
finally
{
IsBusy = false;
}
}
public async Task ExecuteSaveCommandCommand()
{
IsBusy = true;
try
{
_categoryService.addCategory(categoryName);
CategoryNameEditor = string.Empty;
await _navigationService.NavigateAsync("AddCategoryPage");
}
catch (Exception ex)
{
await _toastService.DisplayToastAsync(ex.Message);
}
finally
{
IsBusy = false;
}
}
public void OnAppearing()
{
IsBusy = true;
}
}
}
xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:student_app.ViewModels"
x:Class="student_app.Views.AddCategoryPage"
xmlns:model="clr-namespace:student_app.Models" >
<RefreshView x:DataType="local:AddCategoryViewModel" Command="{Binding LoadCategoriesCommand}" IsRefreshing="{Binding IsBusy, Mode=TwoWay}">
<StackLayout>
<Label Text="Add Category:"/>
<Editor Text="{Binding CategoryNameEditor}" Placeholder="enter category name"/>
<Button x:Name="saveButton" Text="Save" Command="{Binding SaveCommand}"/>
</StackLayout>
</RefreshView>
</ContentPage>
code behind:
namespace student_app.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class AddCategoryPage : ContentPage
{
AddCategoryViewModel _viewModel;
public AddCategoryPage()
{
InitializeComponent();
this.BindingContext = _viewModel = AppContainer.Resolve<AddCategoryViewModel>();
}
protected override void OnAppearing()
{
base.OnAppearing();
_viewModel.OnAppearing();
}
}
}
post method(tokenservice is working):
public async void addCategory(string categoryName)
{
try
{
await _backend.PostAsync<Boolean>($"{_appSettings.PlannedActivityBackendBaseUrl}/Category/Add/{categoryName}", _tokenProvider.AuthAccessToken);
}
catch (BackendAuthenticationException)
{
await _navigationService.NavigateAsync("LoginPage");
}
}