NullReferenceException on page load

I load a list from the database SQLitewhen my page loads, and sometimes when it loads, I get NullReferenceExceptionan error messageObject reference not set to an instance of an object.

it breaks into this code in the SQLite class file

public TableMapping GetMapping (Type type)
{
    if (_mappings == null) {
        _mappings = new Dictionary<string, TableMapping> ();
    }
    TableMapping map;
    if (!_mappings.TryGetValue (type.FullName, out map)) {
        map = new TableMapping (type);
        _mappings [type.FullName] = map; //null here
    }
    return map;
}

This is what I do when my page loads.

public MainPage()
{
    this.InitializeComponent();
    Loaded += MainPage_Loaded;
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    createDatabase();
    getBowlers();
}

private async void createDatabase()
{
    SQLiteAsyncConnection conn = new SQLiteAsyncConnection(BOWLERS_DATABASE);
    await conn.CreateTableAsync<Bowler>();
    conn = new SQLiteAsyncConnection(GAMES_DATABASE);
    await conn.CreateTableAsync<Games>();
}

private async void getBowlers()
{
    SQLiteAsyncConnection conn = new SQLiteAsyncConnection(BOWLERS_DATABASE);

    var query = conn.Table<Bowler>();
    itemListView.DataContext = await query.ToListAsync();
}

I'm new to the page life cycle, but it seems like I'm trying to pull out of a database early perhaps?

EDIT

Stacktrace

System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=mscorlib
StackTrace:
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Collections.Generic.Dictionary`2.set_Item(TKey key, TValue value)
   at SQLite.SQLiteConnection.GetMapping(Type type) in c:\Users\Jeff\Dropbox\Tournament Director Windows\Tournament Director Windows\SQLite.cs:line 231
   at SQLite.TableQuery`1..ctor(SQLiteConnection conn) in c:\Users\Jeff\Dropbox\Tournament Director Windows\Tournament Director Windows\SQLite.cs:line 2129
   at SQLite.SQLiteConnection.Table[T]() in c:\Users\Jeff\Dropbox\Tournament Director Windows\Tournament Director Windows\SQLite.cs:line 616
   at SQLite.SQLiteAsyncConnection.Table[T]() in c:\Users\Jeff\Dropbox\Tournament Director Windows\Tournament Director Windows\SQLiteAsync.cs:line 260
   at Tournament_Director_Windows.MainPage.<getBowlers>d__c.MoveNext() in c:\Users\Jeff\Dropbox\Tournament Director Windows\Tournament Director Windows\MainPage.xaml.cs:line 116
InnerException: 
+2
source share
3 answers

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

Note:

  • The exception is selected from the dictionary code, and not from your (user) code
  • NullReferenceException, ArgumentNullException, null

. , , .

1

, async SQLite.

+4

, GetMapping . , .

, , GetMapping SQLite.cs

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;
    }
}
+1

Try the following:

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

_mappings [type.FullName] = map;
0
source

All Articles