gpt4 book ai didi

c# - 页面加载时出现 NullReferenceException

转载 作者:行者123 更新时间:2023-12-02 04:58:26 30 4
gpt4 key购买 nike

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

它在 SQLite 类文件中的这段代码中中断

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

这是我在加载页面时所做的

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();
}

我是页面生命周期的新手,但似乎我正试图从数据库中拉出到可能的早期?

编辑

堆栈跟踪

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:

最佳答案

根据异常和提供的堆栈跟踪,我认为您的问题就是所描述的here .

注意:

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

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

更新 1

好像there is a bug在 SQLite 异步处理中。

关于c# - 页面加载时出现 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17538275/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com