Litedb: Cryptic error message for ID field problem

Created on 1 Aug 2019  路  6Comments  路  Source: mbdavid/LiteDB

I got the following error: "Object reference not set to an instance of an object"
when bulk inserting objects.

It took some time to figure out, that the problem is that the Id field has only a getter, but no setter.
Like:

public int Id { get; }

This fixed it:

public int Id { get; set; }

Would it be possible to display a user friendly error in this case?
thx

bug

All 6 comments

@bolner Which version did you use? v4.1.4?
Anyways, I'll try to reproduce it.

Hi, the version is 4.1.4. I've just reproduced it at home on Windows. (At work I use Ubuntu.)
Happens both when passing a List and also when an array to the bulk insert. It also doesn't matter which bulk size one chooses, 100K or only 5K. The journaling is disabled. There is a normal, non-unique index on one of the fields.

@bolner Could you provide me with some code to reproduce this as I'm unable to reproduce it myself. At the time of writing, I'm using the code below.

public class Program
{
    static void Main(string[] args)
    {
        var connectionString = new ConnectionString(@"LiteDBIssue1269.db")
        {
            Mode = FileMode.Exclusive
        };

        using (var db = new LiteDatabase(connectionString))
        {
            var collection = db.GetCollection<DummyClass>();

            var list = new List<DummyClass> {new DummyClass(1), new DummyClass(2)};
            collection.InsertBulk(list);

            var debug = collection.FindAll().ToList();

            Console.WriteLine("Pausing here.");
            Console.ReadKey();
        }

        Console.WriteLine("End.");
        Console.ReadKey();
    }

    class DummyClass
    {
        public int Id { get; }

        public DummyClass()
        {
        }

        public DummyClass(int id)
        {
            Id = id;
        }
    }
}

Hi, the following code reproduces it. When the setter is given, then LiteDB automatically puts an auto-increment ID into the Id field.

using System.Collections.Generic;
using LiteDB;

namespace IssueRepro {
    class Program {
        class MyClass {
            public int Id { get; }
            public int Code { get; set; }
            public double Parameter { get; set; }

            public MyClass() { }

            public MyClass(int code, double parameter) {
                this.Code = code;
                this.Parameter = parameter;
            }
        }

        static void Main() {
            using (var db = new LiteDatabase($"filename=db.bin; journal=false")) {
                var collection = db.GetCollection<MyClass>("classes");

                var list = new List<MyClass> {
                    new MyClass(123, 34.56),
                    new MyClass(234, 56.67)
                };

                collection.InsertBulk(list);
            }
        }
    }
}

This is the exact exception:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at LiteDB.LiteCollection`1.GetBsonDocs(IEnumerable`1 documents)+MoveNext()
   at LiteDB.LinqExtensions.YieldBatchElements[T](IEnumerator`1 source, Int32 batchSize)+MoveNext()
   at LiteDB.LiteEngine.<>c__DisplayClass22_0.<Insert>b__0(CollectionPage col)
   at LiteDB.LiteEngine.Transaction[T](String collection, Boolean addIfNotExists, Func`2 action)
   at LiteDB.LiteEngine.InsertBulk(String collection, IEnumerable`1 docs, Int32 batchSize, BsonType autoId)
   at LiteDB.LiteCollection`1.InsertBulk(IEnumerable`1 docs, Int32 batchSize)
   at IssueRepro.Program.Main() in C:\projects\issue_repro\Program.cs:line 28

@mbdavid This issue also occurs on v5 and with the normal .Insert(...) method.

Hi @bolner, just wanted to let you know that it will be possible in a future release of v5. Functionality has been added in commit d65b888a69c6dd6112df1d6644f0fa080711229b (pushed about 20 mins ago) and will be available in the next release of v5.

Therefor, I'll be closing the issue if you don't mind. Feel free to re-open the issue if you feel like it's still needed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rstat1 picture rstat1  路  3Comments

dangershony picture dangershony  路  3Comments

kuiperzone picture kuiperzone  路  4Comments

GW-FUB picture GW-FUB  路  3Comments

muhamad picture muhamad  路  3Comments