Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Empty value in indexed field throws exception #1597

Closed
utech626 opened this issue Mar 28, 2020 · 4 comments
Closed

[BUG] Empty value in indexed field throws exception #1597

utech626 opened this issue Mar 28, 2020 · 4 comments
Labels

Comments

@utech626
Copy link

Version
5.05
Framework Version 4.8

Describe the bug
When inserting a record with a Empty String in a indexed field and exception is thrown.

Code to Reproduce
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using LiteDB;

namespace LiteDBBug
{
public class MyRecord
{
[BsonId]
public int Id { get; set; }
public String Field11 { get; set; }
public String Field22 { get; set; }
public String Field33 { get; set; }
}

class Program
{
	static void Main(string[] args)
	{
		using (LiteDatabase _db = new LiteDatabase(@"MyData.db"))
		{
			var _col = _db.GetCollection<MyRecord>("records");

			_col.EnsureIndex(x => x.Field22, unique: false);

			var _rec1 = new MyRecord
			{
				Field11 = "value1",
				Field22 = "value2",
				Field33 = "value3"
			};
			//
			//	**Success** 
			//
			_col.Insert(_rec1);

			var _rec2 = new MyRecord
			{
				Field11 = "value1",
				Field22 = "",
				Field33 = "value3"
			};
			//
			//	**null reference exception**
			//
			_col.Insert(_rec2);
		}
	}
}

}

Expected behavior
Record should be stored into database

Screenshots/Stacktrace
System.NullReferenceException: Object reference not set to an instance of an object.
at LiteDB.BsonValue.GetHashCode()
at System.Collections.Generic.GenericEqualityComparer1.GetHashCode(T obj) at System.Linq.Set1.InternalGetHashCode(TElement value)
at System.Linq.Set1.Find(TElement value, Boolean add) at System.Linq.Enumerable.<DistinctIterator>d__641.MoveNext()
at LiteDB.Engine.LiteEngine.InsertDocument(Snapshot snapshot, BsonDocument doc, BsonAutoId autoId, IndexService indexer, DataService data)
at LiteDB.Engine.LiteEngine.<>c__DisplayClass7_0.b__0(TransactionService transaction)
at LiteDB.Engine.LiteEngine.AutoTransaction[T](Func2 fn) at LiteDB.Engine.LiteEngine.Insert(String collection, IEnumerable1 docs, BsonAutoId autoId)
at LiteDB.LiteCollection`1.Insert(T entity)
at LiteDBBug.Program.Main(String[] args) in C:\TFSSrc\Test\LiteDBBug\LiteDBBug\Program.cs:line 50

@utech626 utech626 added the bug label Mar 28, 2020
@seertenedos
Copy link

that was the cause of my issue and has been fixed but no release made yet.
#1592 (comment)

@riksking
Copy link

riksking commented Apr 7, 2020

I got same error NRE.

My case

public static void WriteToExistDb(IEnumerable<object> chunk, string pathToDb, string tableName)
        {
            using (var testDb = new LiteDatabase(pathToDb))
            {
                var col = testDb.GetCollection<object>(tableName);
                foreach (var o in chunk)
                {
                    col.Insert(o);
                }
                //col.InsertBulk(chunk);
            }
        }

Call method like this

var words = new List<string>(chunk);

WriteToExistDb(words, _pathToDb, _wordsTableName);

end got error

System.NullReferenceException: 'Object reference not set to an instance of an object.'
at LiteDB.Engine.LiteEngine.InsertDocument(Snapshot snapshot, BsonDocument doc, BsonAutoId autoId, IndexService indexer, DataService data) in ~\LiteDB-master\LiteDB\Engine\Engine\Insert.cs:line 48
at LiteDB.Engine.LiteEngine.<>c__DisplayClass7_0.b__0(TransactionService transaction) in ~\LiteDB-master\LiteDB\Engine\Engine\Insert.cs:line 33
at LiteDB.Engine.LiteEngine.AutoTransaction[T](Func2 fn) in ~\LiteDB-master\LiteDB\Engine\Engine\Transaction.cs:line 101 at LiteDB.Engine.LiteEngine.Insert(String collection, IEnumerable1 docs, BsonAutoId autoId) in ~\LiteDB-master\LiteDB\Engine\Engine\Insert.cs:line 20
at LiteDB.LiteCollection1.Insert(T entity) in ~\LiteDB-master\LiteDB\Client\Database\Collections\Insert.cs:line 20 at Avtomat.Utils.LiteDbDataAccess.WriteToExistDb(IEnumerable1 chunk, String pathToDb, String tableName)

As you can see, I was build from master.

I was debug LiteDb and find that in this place

image

I get null in BsonDocument object.

next steps in debugger

image

@riksking
Copy link

riksking commented Apr 7, 2020

With InsertBulk, I got similar error.

System.NullReferenceException: 'Object reference not set to an instance of an object.'
at LiteDB.Engine.LiteEngine.InsertDocument(Snapshot snapshot, BsonDocument doc, BsonAutoId autoId, IndexService indexer, DataService data) in ~\LiteDB-master\LiteDB\Engine\Engine\Insert.cs:line 48
at LiteDB.Engine.LiteEngine.<>c__DisplayClass7_0.b__0(TransactionService transaction) in ~\LiteDB-master\LiteDB\Engine\Engine\Insert.cs:line 33
at LiteDB.Engine.LiteEngine.AutoTransaction[T](Func2 fn) in ~\LiteDB-master\LiteDB\Engine\Engine\Transaction.cs:line 101 at LiteDB.Engine.LiteEngine.Insert(String collection, IEnumerable1 docs, BsonAutoId autoId) in ~\LiteDB-master\LiteDB\Engine\Engine\Insert.cs:line 20
at LiteDB.LiteCollection1.InsertBulk(IEnumerable1 entities, Int32 batchSize) in ~\LiteDB-master\LiteDB\Client\Database\Collections\Insert.cs:line 66
at Avtomat.Utils.LiteDbDataAccess.WriteToExistDb(IEnumerable`1 chunk, String pathToDb, String tableName)

@lbnascimento
Copy link
Contributor

@riksking Your issue is very different.

First of all, you're opening your collection with GetCollection<object>. While this is technically supported, it is not recommended: the point of GetCollection<T> is that you set T to the C# class you're trying to store (Person, Customer, Account...) and the mapper converts your class to a BsonDocument upon insertion and converts it back to T upon retrieval.

The error is happening because you're trying to store strings in your LiteCollection<object>, and the mapper doesn't know how to map a string to a BsonDocument.

I'm going to close this issue, since the original problem is already fixed in the master. If you have any questions, please open another issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants