Xamrin.Forms SQL shared project, something throwing null and i can't find it.

Daedric1991Daedric1991 AUMember ✭✭
edited August 2018 in Xamarin.Forms

I had this working for most of the project and not sure why it stopped working but im getting a null exception error when this line is used.

This line is in the App file.

database = new PlacesDatabase(DependencyService.Get<IFileHelper>().GetLocalFilePath("CoastlineSQLite.db"));

This is the line in the menu.xaml.cs file that triggers it.

List<PlaceItem> places = await App.Database.GetTowns();

I have no idea what has gone wrong as i haven't adjusted accessing the database. I checked the resources file and the CoastlineSQLite.db file is there like it should be.

I am using the example from here: https://arteksoftware.com/deploying-a-database-file-with-a-xamarin-forms-app/ to help me and it was working a few days ago.

[assembly: Dependency(typeof(FileHelper))]
namespace CoastlineNews.iOS
{
    public class FileHelper : IFileHelper
    {
        public string GetLocalFilePath(string filename)
        {
            string docFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            string libFolder = Path.Combine(docFolder, "..", "Library", "Databases");

            if (!Directory.Exists(libFolder))
            {
                Directory.CreateDirectory(libFolder);
            }

            string dbPath = Path.Combine(libFolder, filename);

            CopyDatabaseIfNotExists(dbPath);

            return Path.Combine(libFolder, filename);
        }

        private static void CopyDatabaseIfNotExists(string dbPath)
        {
            // add if DB from existingdb is newer to overwrite old DB.
            //if (!File.Exists(dbPath))
            //{
                var existingDb = NSBundle.MainBundle.PathForResource("CoastlineSQLite", "db");

                File.Delete(dbPath);
                File.Copy(existingDb, dbPath);
            //}
        }
    }
}

The main issue is I can't figure out where to go from here to find the source of this null exception. Where do I go from here in the debugging process?

Also important note is that I ran the android version and it works just fine, i coppied the exact same DB file over for the android version, so it is probably something in the iOS specific code that is causing it to break. I have the !file.exists commented out to always overwrite the old DB, i tried with it not commented out encase db was deleted and the file.delete was actually causing it but that didn't work. both return a null error.

Best Answer

  • Daedric1991Daedric1991 AU ✭✭
    Accepted Answer

    fixed, somehow one of the iOS files had their build action from Compile to None.

Answers

  • seanydaseanyda GBMember ✭✭✭✭✭

    Nothing jumps out at me that there is an issue in that code, however it looks like quite a bad way of handling the database.

    If you've been modifying code, running the app, testing, repeat there is a chance the files are just all over the place in the local file system. If you delete the app from your emulator/simulator and start again fresh you can debug the app line by line and find exactly where the program isn't doing what you expect.

  • Daedric1991Daedric1991 AUMember ✭✭

    @seanyda said:
    Nothing jumps out at me that there is an issue in that code, however it looks like quite a bad way of handling the database.

    If you've been modifying code, running the app, testing, repeat there is a chance the files are just all over the place in the local file system. If you delete the app from your emulator/simulator and start again fresh you can debug the app line by line and find exactly where the program isn't doing what you expect.

    I tried that. didn't work. Also, what is wrong with the way it is being handled atm?

    I don't get how it broke either. I was updating and changing the database. I updated the objects related to the database as well and they match just fine as well as the fact I can launch it on Android and the same database file works just fine! I didn't edit any code that was restricted to iOS.

    https://www.dropbox.com/sh/9sqfgl3q9rlngkx/AABwFwZr6uYCFPudTQTUKDV9a?dl=0

    here is a link to the dropbox folder so you can take a look at the raw code.

  • Daedric1991Daedric1991 AUMember ✭✭
    Accepted Answer

    fixed, somehow one of the iOS files had their build action from Compile to None.

Sign In or Register to comment.