页面加载时出现 NullReferenceException [英] NullReferenceException when page Loads

查看:29
本文介绍了页面加载时出现 NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的页面加载时,我从 SQLite 数据库加载一个列表,有时加载时我收到 NullReferenceException 并显示错误说 Object reference not set to an一个对象的实例.

I load a list from a SQLite database when my page loads and sometimes when it loads I get NullReferenceException with the error saying Object reference not set to an instance of an object.

它在 SQLite 类文件中破坏了这段代码

it breaks in 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 am new to page lifecycle but it seems that I am trying to pull from the database to early possibly?

编辑

堆栈跟踪

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: 

推荐答案

基于异常和提供的堆栈跟踪,我认为您的问题是此处.

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

注意:

  1. 异常是从字典代码中抛出的,而不是从你的(用户)代码中抛出的
  2. 异常是 NullReferenceException,而不是 ArgumentNullException,所以不只是参数为 null

字典 不是线程安全的.这意味着如果它被多个线程访问,则对它的访问应该是同步的.

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

更新 1

似乎SQLite 异步处理中存在错误.

这篇关于页面加载时出现 NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆