0

I´m getting a System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.' when i try to connect to SQLite, but i don´t know how to solve this problem. My code:

public class SQLiteContactStore : IContactStore
{
    private SQLiteAsyncConnection _connection;

    public SQLiteContactStore(ISQLiteDb db)
    {
        _connection = db.GetConnection();
        _connection.CreateTableAsync<Contact>();
    }

    public async Task<IEnumerable<Contact>> GetContactsAsync()
    {
        return await _connection.Table<Contact>().ToListAsync();
    }

    public async Task DeleteContact(Contact contact)
    {
        await _connection.DeleteAsync(contact);
    }

    public async Task AddContact(Contact contact)
    {
        await _connection.InsertAsync(contact);
    }

    public async Task UpdateContact(Contact contact)
    {
        await _connection.UpdateAsync(contact);
    }

    public async Task<Contact> GetContact(int id)
    {
        return await _connection.FindAsync<Contact>(id);
    }
}

Exception comes in the line _connection = db.GetConnection();

My call stack shows this:

0x8 in APPv1.SQLiteContactStore..ctor at C:\Users\Fidel\Desktop\APPBUENAv1\APPv1\APPv1\Persistence\SQLiteContactStore.cs:16,13

that comes from:

public interface ISQLiteDb
{
    SQLiteAsyncConnection GetConnection();
}

and this:

0xE in APPv1.Views.ContactsPage..ctor at C:\Users\Fidel\Desktop\APPBUENAv1\APPv1\APPv1\Views\ContactsPage.xaml.cs:12,13

that comes from:

public ContactsPage()
    {
        var contactStore = new SQLiteContactStore(DependencyService.Get<ISQLiteDb>());
        var pageService = new PageService();

        ViewModel = new ContactsPageViewModel(contactStore, pageService);

        InitializeComponent();
    }

Sorry, i´m a newbie...

Could you help me find the solution?

Thanks in advance.

Fidel
  • 19
  • 4
  • Show the code where you're calling this constructor. – Broots Waymb Apr 17 '20 at 17:27
  • 3
    is db null? Is GetConnection() your code or from some library? What dos the stack trace show? – Jason Apr 17 '20 at 17:27
  • 3
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Code Stranger Apr 17 '20 at 17:28
  • 2
    Post the entire stack trace. You can get this and additional exception information by calling `ToString()` on the thrown exception instance. Add *all* of that to your question. – Igor Apr 17 '20 at 17:33
  • I have edited my question with more information... – Fidel Apr 17 '20 at 19:07

0 Answers0