gpt4 book ai didi

c# - 查询映射的 CustomObject 时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 09:45:26 24 4
gpt4 key购买 nike

我有以下代码:

        ActiveModule resultObject = null;

StringBuilder sqlStringBuilder = new StringBuilder();
sqlStringBuilder.AppendLine("SELECT * FROM suite_activemodule as _module");
sqlStringBuilder.AppendLine("WHERE CityID = @CityID AND BaseModuleID = @BaseModuleID");

using (MySqlConnection connection = new MySqlConnection(ConnectionString))
{
var _resultList = connection.Query<ActiveModule>(sqlStringBuilder.ToString(), new { CityID = cityID, ModuleID = moduleID }, null, true, null, null); //<- This is were I get the error...
int listSize = _resultList.Count();
if (_resultList != null && !_resultList.Count().Equals(0))
{
resultObject = _resultList.ToList().First();
resultObject.Module = BaseModuleRepository.GetByID(resultObject.ModuleID);
}
}

查询本身工作正常,但当我尝试运行查询时,我收到以下错误消息:

bei MySql.Data.MySqlClient.MySqlConnection.get_ServerThread()

bei MySql.Data.MySqlClient.MySqlConnection.Abort()

bei MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)

bei Dapper.SqlMapper.ExecuteReaderWithFlagsFallback(IDbCommand cmd, Boolean wasClosed, CommandBehavior behavior) in C:\projects\dapper\Dapper\SqlMapper.cs:Zeile 1053.

bei Dapper.SqlMapper.d__138`1.MoveNext() in C:\projects\dapper\Dapper\SqlMapper.cs:Zeile 1081.

bei System.Collections.Generic.List1..ctor(IEnumerable1 collection)

bei System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

bei Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable1
commandTimeout, Nullable
1 commandType) in C:\projects\dapper\Dapper\SqlMapper.cs:Zeile 723.

...Repository.GetByCityIDAndModuleID(Int64 cityID, Int64 moduleID)

...DeactivateModule(String moduleID)

但是我之前运行过多个此类查询,但没有收到此错误。这没有多大意义,因为使用过的东西都不是真正的空。

我已经完成了一个干净的构建,并且还通过 Visual Studio 进行了完整的重建。

有人知道这个问题并知道如何解决吗?

最佳答案

正如评论中提到的,可以通过在对 Query<>() 的调用中分配“BaseModuleID”来修复 NullReferenceException,如下所示:

var _resultList = connection.Query<ActiveModule>(sqlStringBuilder.ToString(), new { CityID = cityID, BaseModuleID = moduleID }, null, true, null, null);

另一个建议:您调用 _resultList.Count() 两次。如果您的查询返回大量结果,这可能会很昂贵。由于您只处理列表中的第一项,因此可以在列表上使用 FirstOrDefault() 而不是 Count()。或者,更好的是,您可以在 MySqlConnection 上使用 QueryFirstOrDefault<>(),如下所示:

    ActiveModule resultObject = null;
StringBuilder sqlStringBuilder = new StringBuilder();
sqlStringBuilder.AppendLine("SELECT * FROM suite_activemodule as _module");
sqlStringBuilder.AppendLine("WHERE CityID = @CityID AND BaseModuleID = @BaseModuleID");

using (MySqlConnection connection = new MySqlConnection(ConnectionString))
{
resultObject = connection.QueryFirstOrDefault<ActiveModule>(sqlStringBuilder.ToString(), new { CityID = cityID, BaseModuleID = moduleID });
if (resultObject != null)
{
resultObject.Module = BaseModuleRepository.GetByID(resultObject.ModuleID);
}
}

祝你好运!

关于c# - 查询映射的 CustomObject 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55774872/

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