Bug #5496 Yet Another "object reference not set to an instance of an object"
Submitted: 9 Sep 2004 16:48 Modified: 22 Oct 2004 20:09
Reporter: Puiu Hrenciuc Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:1.0.0-Beta OS:Windows (Windows XP Pro SP2)
Assigned to: Reggie Burnett CPU Architecture:Any

[9 Sep 2004 16:48] Puiu Hrenciuc
Description:
When trying to read a BIGINT column with GetInt16(), GetInt32(),
GetUInt16() or GetUInt32() an exception is thrown 
("object reference not set to an instance of an object").

How to repeat:
// Stripped exceptions handling code

using System;
using MySql.Data.MySqlClient;

namespace MySQLTest
{
	class clsMySQLTest
	{
		[STAThread]
		static void Main(string[] args)
		{
			string SQL;
			MySqlConnection Connection=new MySqlConnection("Username=#####;Password=#####;Port=3306;Host=localhost;Database=test");
			MySqlCommand Command;

			SQL="CREATE TABLE IF NOT EXISTS test_bigint (t BIGINT)";
			Command=new MySqlCommand(SQL,Connection);

			SQL="INSERT INTO test_bigint(t) VALUES('1')";
			Command=new MySqlCommand(SQL,Connection);

			MySqlDataReader Rows;
			SQL="SELECT t FROM test_bigint";
			Command=new MySqlCommand(SQL,Connection);

			while (Rows.Read())
			{
				// Next line throws "Object reference not set to an instance of an object."
				Console.WriteLine("Value : {0}",Rows.GetInt16(0));
			}
		}
	}
}

Suggested fix:
I don't know if this should be considered a bug since you can't fit a bigint into 
16 or 32 bits, but it would be better if a MySqlException would be thrown with
a message like "Incorrect column type for GetInt16()", or the returned value is
rounded to 16/32 bits. It took me some time to realise the cause of the error so others may have some hard times looking for the cause.
[9 Sep 2004 16:56] Puiu Hrenciuc
I forgot to say that this problem does not affect ByteFX 0.76
[9 Sep 2004 16:59] Puiu Hrenciuc
Sorry, I stripped the command execute functions when removed the exception 
handling code. So Actually this is the code :

//Stripped the exception handling code
using System;
using MySql.Data.MySqlClient;

namespace MySQLTest
{
	class clsMySQLTest
	{
		[STAThread]
		static void Main(string[] args)
		{
			string SQL;
			MySqlConnection Connection=new MySqlConnection("Username=#####;Password=#####;Port=3306;Host=localhost;Database=test");
			MySqlCommand Command;
			Connection.Open();

			SQL="CREATE TABLE IF NOT EXISTS test_bigint (t BIGINT)";
			Command=new MySqlCommand(SQL,Connection);
			Command.ExecuteNonQuery();

			SQL="INSERT INTO test_bigint(t) VALUES('1')";
			Command.ExecuteNonQuery();

			MySqlDataReader Rows;
			SQL="SELECT t FROM test_bigint";
			Rows=Command.ExecuteReader();
			
			while (Rows.Read())
			{
				// Next line throws "Object reference not set to an instance of an object."
				Console.WriteLine("Value : {0}",Rows.GetDouble(0));
			}
		}
	}
}
[21 Oct 2004 10:50] Hartmut Holzgraefe
had to tweak the code a bit as this time the Command instanciations were missing,
i reproduced the error with the code below and i have to agree that the exception
description is indeed a bit missleading

using System;
using MySql.Data.MySqlClient;

namespace MySQLTest
{
        class clsMySQLTest
        {
                [STAThread]
                static void Main(string[] args)
                {
                        string SQL;
                        MySqlConnection Connection=new
MySqlConnection("Username=root;Password=;Port=3306;Host=localhost;Database=test");
                        MySqlCommand Command;
                        Connection.Open();

                        SQL="DROP TABLE IF EXISTS test_bigint";
                        Command=new MySqlCommand(SQL,Connection);
                        Command.ExecuteNonQuery();

                        SQL="CREATE TABLE test_bigint (t BIGINT)";
                        Command=new MySqlCommand(SQL,Connection);
                        Command.ExecuteNonQuery();

                        SQL="INSERT INTO test_bigint(t) VALUES('1')";
                        Command=new MySqlCommand(SQL,Connection);
                        Command.ExecuteNonQuery();

                        MySqlDataReader Rows;
                        SQL="SELECT t FROM test_bigint";
                        Command=new MySqlCommand(SQL,Connection);
                        Rows=Command.ExecuteReader();

                        while (Rows.Read())
                        {
                                // OK
                                Console.WriteLine("Value as Int64: {0}",Rows.GetInt64(0));
                                // fails as BIGINT needs GetInt64, not 32
                                Console.WriteLine("Value as Int32: {0}",Rows.GetInt32(0));
                        }
                }
        }
}
[22 Oct 2004 20:09] Reggie Burnett
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html

Additional info:

This has been improved.  It now throws an InvalidCastException in all cases except GetString().  GetString() should always return a string, right or wrong.