【发布时间】:2013-07-06 11:20:10
【问题描述】:
当我的页面加载时,我从SQLite 数据库加载一个列表,有时当它加载时我得到NullReferenceException,错误提示为Object reference not set to an instance of an object.
它在 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:
【问题讨论】:
-
你在哪一行得到 NullReferenceException?
-
我在代码
_mappings [type.FullName] = map;中注释了 -
是
_mappings一个静态变量和/或被多个线程调用? -
@ScottChamberlain 我从 NuGet 扩展下载的 SQLite 库中的所有内容
-
@ScottChamberlain 它是一个类范围的变量,但不是静态的
private Dictionary<string, TableMapping> _mappings = null;