Hi there,
This is my first attempt at creating an app using Xamarin Forms so apologies in advance if this is a simple fix, but I have searched in depth and cannot appear to find a solution to my error.
I am creating an SQLite database following the tutorial "Working with Local Db (SQLite) in Xamarin Forms" by Dheeraj Kumar G, with the only difference I have made is using my own input page with different inputs instead of the one he has created at 38.52 in the video. When I enter details in to my input page I get the following error - "System.NullReferenceException Object reference not set to an instance of an object", when I click the button to save my input to the database. The stack trace then points me to line 30 below which I have indicated with the arrow:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BudgettingApp.Model;
using SQLite;
namespace BudgettingApp
{
public class ExpenseDatabase
{
readonly SQLiteAsyncConnection database;
public ExpenseDatabase(string dbPath)
{
database = new SQLiteAsyncConnection(dbPath);
database.CreateTableAsync<ExpenseDetail>().Wait();
}
public Task<List<ExpenseDetail>> GetExpensesAsync()
{
return database.Table<ExpenseDetail>().ToListAsync();
}
public Task<ExpenseDetail> GetExpenseDetailAsync(int id)
{
return database.Table<ExpenseDetail>().Where(i => i.ExpenseID == id).FirstOrDefaultAsync();
}
public Task<int> SaveExpenseDetailAsync(ExpenseDetail expense)
{
if(expense.ExpenseID == 0) { **## <-----------------**
return database.InsertAsync(expense);
}
else {
return database.UpdateAsync(expense);
}
}
public Task<int> DeleteExpenseDetailAsync(ExpenseDetail expense)
{
return database.DeleteAsync(expense);
}
}
}
And then the second place Stacktrace points me to is line 21 on my Input c# page:
using System;
using System.Collections.Generic;
using BudgettingApp.Model;
using Xamarin.Forms;
namespace BudgettingApp.View
{
public partial class OutgoingsPage : ContentPage
{
public OutgoingsPage()
{
InitializeComponent();
}
async void Save_Clicked(object sender, System.EventArgs e)
{
if (CategoryPicker.SelectedIndex != -1)
{
var newExpense = (ExpenseDetail)BindingContext;
await App.Database.SaveExpenseDetailAsync(newExpense); ## ** <----------------**
await DisplayAlert("Expense saved!", "Tap to continue", "Ok");
}
}
private void CategoryPicker_SelectedIndexChanged(object sender, EventArgs e)
{
CategoryPicker.IsEnabled = true;
}
private void PaymentPicker_SelectedIndexChanged(object sender, EventArgs e)
{
PaymentPicker.IsEnabled = true;
}
}
}
I am not sure what I am looking for here, I have tried simplifying my input by removing the CategoryPickers etc but it did not help.
And finally here is the code for the input I am trying to save to:
using System;
using SQLite;
namespace BudgettingApp.Model
{
public class ExpenseDetail
{
[PrimaryKey, AutoIncrement]
//[Display(AutoGenerateField = false)]
public int ExpenseID { get; set; }
public string ExpenseName { get; set; }
public string Category { get; set; }
public DateTime Date { get; set; }
public double Spent { get; set; }
public string PaymentMethod { get; set; }
public Boolean Recurring { get; set; }
}
}
I hope this post is okay, like I said it is my first attempt with Xamarin Forms so apologies if this is unclear, if any further code/explanation is required please let me know. Alternatively if there is a different method you
Thanks.