简体   繁体   中英

Connecting MySql in Unity3d -> NullReferenceException: Object reference not set to an instance of an object

Here is my code in Visual Studio connected to Unity3d:

MySqlConnection connection;

MySqlConnectionStringBuilder connBuilder = new MySqlConnectionStringBuilder();

                connBuilder.Server = "localhost";
                connBuilder.UserID = "root";
                connBuilder.Database = "therapygame";
                connBuilder.Password = "";
                connBuilder.OldGuids = true;

                connection = new MySqlConnection(connBuilder.ConnectionString);

Here is the error in Unity:

NullReferenceException: Object reference not set to an instance of an object
System.Data.Common.DbConnectionStringBuilder.Remove (System.String keyword)
MySql.Data.MySqlClient.MySqlConnectionStringBuilder.Remove (System.String keyword)
MySql.Data.MySqlClient.MySqlConnectionStringOption.Clean (MySql.Data.MySqlClient.MySqlConnectionStringBuilder builder)
MySql.Data.MySqlClient.MySqlConnectionStringBuilder.SetValue (System.String keyword, System.Object value)
MySql.Data.MySqlClient.MySqlConnectionStringOption.<.ctor>b__0 (MySql.Data.MySqlClient.MySqlConnectionStringBuilder msb, MySql.Data.MySqlClient.MySqlConnectionStringOption sender, System.Object value)
MySql.Data.MySqlClient.MySqlConnectionStringBuilder.set_Item (System.String keyword, System.Object value)
MySql.Data.MySqlClient.MySqlConnectionStringBuilder.set_Server (System.String value)
mysql.SetupSQLConnection () (at Assets/mysql.cs:31)
fire_rate.Start () (at Assets/fire_rate.cs:18)

Line 31 of mysql.cs:

connBuilder.Server = "localhost";

Line 18 of fire_rate.cs:

GameObject.Find("MySQL").GetComponent<mysql>().SetupSQLConnection();

This post is an update on a previous issue. Changes in code were made via an answer on that post. Here is the link: MySqlConnection = new MySqlConnection(string) not working

This worked!

It seems very odd to be getting a NullReferenceException (which usually indicates a bug) in DbConnectionStringBuilder.Remove (which is a Framework library) from calling MySqlConnectionStringBuilder.set_Server (which is a pretty common property). Does Unity provide its own version of that library, I wonder? If you can, try switching to nuget.org/packages/MySqlConnector an alternative OSS driver for MySQL, because it doesn't use the same ConnectionStringBuilder code path that your exception message shows.

@Bradley Grainger

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM