I am trying a simple dapper query using the "GetMessages()" method below but keep running into an "Object reference not set to an instance of an object" error. Sometimes when I restart my computer it will return the full query, but other times it returns the error. I can see that I am connected to the database and that the query starts, however, after loading about 500mb into memory it will randomly stop query and return the error. From what I can tell the connection is never set to null.
Here is the query class:
public class MySqlDatabaseQueries
{
#region Private Properties
private IDbConnection _connection { get; }
private readonly string _reportingMessageQueryString = "Use reporting;\nSELECT *\nFROM message;";
#endregion
#region Public Methods
public IEnumerable<Message> GetMessages() =>
_connection.Query<Message>(_reportingMessageQueryString);
#endregion
#region Constructor
public MySqlDatabaseQueries(IDbConnection connection)
{
_connection = connection;
}
#endregion
}
Here is what is calling the query class
public class Program
{
static void Main(string[] args)
{
// Asks user for user name'
Console.WriteLine();
Console.WriteLine("Please provide your username and password for the database.");
Console.Write("UserName: ");
string userName = Console.ReadLine();
// Ask user for password
Console.Write("Password: ");
string password = Console.ReadLine();
// Data base query variables
IEnumerable<Message> messages;
// Creates the data base connection
MySqlDatabaseHelper dbhelper = new MySqlDatabaseHelper(userName,password);
using (IDbConnection connection = dbhelper.CreateConnection())
{
// Initialize query helper
MySqlDatabaseQueries queryHelper = new MySqlDatabaseQueries(connection);
// Queries the faq table. Joins with the conditions stable to get the KB Article and repository
messages = queryHelper.GetMessages();
}
}
}
Here is the output of the error
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=MySql.Data
StackTrace:
at MySql.Data.MySqlClient.MySqlConnection.get_ServerThread()
at MySql.Data.MySqlClient.MySqlConnection.HandleTimeoutOrThreadAbort(Exception ex)
at MySql.Data.MySqlClient.MySqlDataReader.Read()
at Dapper.SqlMapper.<QueryImpl>d__140`1.MoveNext() in /_/Dapper/SqlMapper.cs:line 1096
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in /_/Dapper/SqlMapper.cs:line 721
at Socrates.ReportingDatabaseFaqTagging.DatabaseHelper.MySqlDatabaseQueries.GetMessages() in C:\Users\grwom\source\repos\Socrates.ReportingDatabaseFaqTagging\Socrates.ReportingDatabaseFaqTagging\DatabaseHelper\MySqlDatabaseQueries.cs:line 34
at Socrates.ReportingDatabaseFaqTagging.Program.Main(String[] args) in C:\Users\grwom\source\repos\Socrates.ReportingDatabaseFaqTagging\Socrates.ReportingDatabaseFaqTagging\Program.cs:line 48
User contributions licensed under CC BY-SA 3.0