NullReferenceException when page Loads

妖精的绣舞 提交于 2019-12-06 09:31:48
Andrew Savinykh

Based on the exception and provided stack trace, I think that your issue is what is described here.

Note:

  1. The exception is thrown from the Dictionary code, not from your (user) code
  2. The exception is NullReferenceException, not ArgumentNullException, so it's not just the argument being null

Dictionary is not thread safe. This means that access to it should be synchronised if it's accessed by several threads.

UPDATE 1

It seems like there is a bug in SQLite async handling.

codingrhythm

The problem with the original code is that the GetMapping method was not thread-safe. Adding a lock, below, forces it to be thread-safe and solves the problem.

Based on the answer and comments, here is the code, change the GetMapping Method in your SQLite.cs file to this

private Object thisLock = new Object(); // add lock obj
public TableMapping GetMapping(Type type, CreateFlags createFlags = CreateFlags.None)
{
    // include original code in the lock block
    lock (thisLock)
    {
        if (_mappings == null)
        {
            _mappings = new Dictionary<string, TableMapping>();
        }
        TableMapping map;
        if (!_mappings.TryGetValue(type.FullName, out map))
        {
            map = new TableMapping(type, createFlags);
            _mappings[type.FullName] = map;
        }
        return map;
    }
}

Try this:

if (!_mappings.ContainsKey(type.FullName))
{
    _mappings.Add(map);
}

_mappings [type.FullName] = map;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!