NullReferenceException using Sqlite in Android - Xamarin.Forms PCL

clamumclamum USMember

I'm working on my first Xamarin.Forms app, a PCL. I just implemented a Sqlite database and used Chapter 7, including its example code, of Dan Hermes' "Xamarin Mobile Application Development: Cross-Platform C# and Xamarin.Forms Fundamentals" book to guide me. The implementation actually looks to be the same as in the Xamarin.Forms "Todo" application, available on Github, with a Database class in each platform target that implements a DbConnect method, which itself basically builds the db path for each platform and creates a new SQLiteConnection with the path.

However, I'm getting a NullReferenceException in Android (I've tried WinPhone 8.1 so far and that works fine), on this line in the PCL's Database class (just a locking object, db instance variable, and constructor):

database = DependencyService.Get<IDatabase>().DbConnect();

I hit F11 to step-in and it told me I didn't have the DependencyService.cs source file. I happened to see the "DependencyService.cs Deep Dive" thread and now that I have the .cs file downloaded, whenever I hit F11 now it just says "Missing Frame Source" or whatever and I can't pick the file.

So. Any ideas on what's going wrong? Any way I can step into the Xamarin.Forms code now that I have the DependencyService.cs file?

Thank you so much for any help.

image

Posts

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I think it's a problem with android implementation . Where is the string 'attribute ' ? Inside or outside namespace?

  • SpaceMonkeySpaceMonkey GBMember ✭✭✭

    Why are you wasting time trying to debug DependencyService.cs?

    Your problem is that you didn't register a native implementation of IDatabase. You said there is a class Database for each platform. You need to register the Android implementation in the native Android project:

    [assembly: Dependency(typeof(IDatabase))]
    namespace Droid
    {
        public class Database : IDatabase
        {
        }
    }
    
  • clamumclamum USMember

    @SpaceMonkey said:
    Why are you wasting time trying to debug DependencyService.cs?

    Your problem is that you didn't register a native implementation of IDatabase. You said there is a class Database for each platform. You need to register the Android implementation in the native Android project:

    [assembly: Dependency(typeof(IDatabase))]
    namespace Droid
    {
    public class Database : IDatabase
    {
    }
    }

    Oh jeez, yeah that was it. Sorry guys, that was a derp mistake.

    I appreciate the help, man.

  • SpaceMonkeySpaceMonkey GBMember ✭✭✭

    @clamum Mark as answer.

  • shubhrakushalshubhrakushal USMember ✭✭

    I have register the native implementation of IDatabase still getting the null refrence exception. Please someone help me out

Sign In or Register to comment.