Snickbrack
Snickbrack

Reputation: 976

NullPointerException while Querying for mapped CustomObject

I have following code:

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

The query itself works fine but as soon as I try to run the query I get the following error message:

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, Nullable1 commandType) in C:\projects\dapper\Dapper\SqlMapper.cs:Zeile 723.

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

...DeactivateModule(String moduleID)

But I am running multiple queries like this kind before and dont get this error. It don't make that much sense because nothing of the used stuff is really null.

I have already did a clean build and also a full rebuild via Visual Studio.

Does anyone know about this issue and know how to fix it?

Upvotes: 0

Views: 1208

Answers (1)

Steve Wong
Steve Wong

Reputation: 2256

As mentioned in the comments, the NullReferenceException can be fixed by assigning the 'BaseModuleID' in your call to Query<>() like this:

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

Another suggestion: you are calling _resultList.Count() twice. If your query returns a large # of results, this could be expensive. Since you are only processing the first item in the list, you could use FirstOrDefault() on the list instead of Count(). Or, even better, you could use QueryFirstOrDefault<>() on the MySqlConnection like this:

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

Good luck!

Upvotes: 1

Related Questions