数据库问题。 object ref未设置为object的实例 [英] Database problem. object ref does not set to instance of object

查看:68
本文介绍了数据库问题。 object ref未设置为object的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上。我遇到了将数据插入mysql数据库的问题。问题表示对象引用没有设置为object的实例。但是,我确实设定了它。这是代码



Hai. I'm having a problem of inserting data to database of mysql. the problem said that the object reference does not set to instance of object. However, I do set it. Here is the code

class saveDatabase
    {
        #region Object Declaration
        protected MySqlConnection conn;
        protected MySqlCommand command;
        protected frmCreateDB dbase = new frmCreateDB();
        #endregion

        #region Local Variables
        protected int i;
        protected string connStr = string.Empty;
        protected string database = string.Empty;
        #endregion

        #region Constructor
        public saveDatabase(frmMain Main)
        {
            database = dbase.getModelName;
            connStr = "datasource=localhost;port=3306;username=root;password=root;";

            conn = new MySqlConnection(connStr);

            command = conn.CreateCommand();
        }
        #endregion
    }

    class saveMetabolites : saveDatabase
    {
        public saveMetabolites(frmMain Main)
            : base(Main)
        {
            //Insert Metabolites Values
            conn.Open();
            for (i = 0; i < Main.dbMetName.Rows.Count; i++)
            {

                command.CommandText = "INSERT INTO " + database +
                                      ".Metabolites " +
                                      "(MetaboliteID, Metabolite_Name)" +
                                      "VALUES " +
                                      "('" + Main.dbMetName.Rows[i].Cells[0].Value.ToString() +
                                      "' , '" + Main.dbMetName.Rows[i].Cells[1].Value.ToString() + "')";
                command.ExecuteNonQuery();
            }
            conn.Close();

            //Insert Dynamic Equations Value
            conn.Open();
            for (i = 0; i < Main.dbDynamicEq.Rows.Count; i++)
            {
                command.CommandText = "UPDATE " + database +
                                      ".Metabolites " +
                                      "SET DynamicEq = " +
                                      "'" + Main.dbDynamicEq.Rows[i].Cells[1].Value.ToString() + "'" +
                                      "WHERE Metabolite_Name = " +
                                      "'" + Main.dbMetName.Rows[i].Cells[1].Value.ToString() + "'";
                command.ExecuteNonQuery();

                command.CommandText = "UPDATE " + database +
                                      ".Metabolites " +
                                      "SET Initial_Condition = " +
                                      "'" + Main.dbDynamicEq.Rows[i].Cells[2].Value.ToString() + "'" +
                                      "WHERE Metabolite_Name = " +
                                      "'" + Main.dbMetName.Rows[i].Cells[1].Value.ToString() + "'";
                command.ExecuteNonQuery();
            }
            conn.Close();
        }
    }

    class saveCoMetabolites : saveDatabase
    {
        public saveCoMetabolites(frmMain Main)
            : base(Main)
        {
            //Insert Co-Metabolites Values
            conn.Open();
            for (i = 0; i < Main.dbCoMetName.Rows.Count; i++)
            {
                command.CommandText = "INSERT INTO " + database +
                                      ".CoMetabolites " +
                                      "(CoMetaboliteID, CoMetabolite_Name)" +
                                      "VALUES " +
                                      "('" + Main.dbCoMetName.Rows[i].Cells[0].Value.ToString() +

                                      "' , '" + Main.dbCoMetName.Rows[i].Cells[1].Value.ToString() + "')";
                command.ExecuteNonQuery();
            }
            conn.Close();
        }
    }

    class saveKineticEq : saveDatabase
    {
        public saveKineticEq(frmMain Main)
            : base(Main)
        {
            //Insert Kinetic Equations Values
            MessageBox.Show(database.ToString());
            conn.Open();
            for (i = 0; i < Main.dbKinetEq.Rows.Count; i++)
            {

                command.CommandText = "INSERT INTO " + database +
                                      ".EnzymeKinetics " +
                                      "(Kinetic_Name, Equations)" +
                                      "VALUES " +
                                      "('" + Main.dbKinetEq.Rows[i].Cells[0].Value.ToString() +
                                      "' , '" + Main.dbKinetEq.Rows[i].Cells[1].Value.ToString() + "')";
                command.ExecuteNonQuery();
            }
            conn.Close();
        }
    }

    class saveKineticPar : saveDatabase
    {
        public saveKineticPar(frmMain Main)
            : base(Main)
        {
            //Insert Kinetic Parameters Values
            conn.Open();
            for (i = 0; i < Main.dbKinetPar.Rows.Count; i++)
            {

                command.CommandText = "INSERT INTO " + database +
                                      ".KineticParameters " +
                                      "(Parameter_Name, Parameter_Value)" +
                                      "VALUES " +
                                      "('" + Main.dbKinetPar.Rows[i].Cells[0].Value.ToString() +
                                      "' , '" + Main.dbKinetPar.Rows[i].Cells[1].Value.ToString() + "')";
                command.ExecuteNonQuery();
            }
            conn.Close();
        }
    }





在主类中,我定义了这样的对象





In the main class, i defined the object like this

saveDatabase svdb;





和类的内部功能我这样做





and inside function of class i do it like this

private bool tryClose(FormClosingEventArgs e)
       {
           DialogResult result = MessageBox.Show("Save Changes?", "Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);

           switch (result)
           {
               case DialogResult.Yes:
                    SaveAs();
                    return false;

               case DialogResult.No:
                    return false;

               default:
                    e.Cancel = true;
                    break;
           }
           return true;
       }







private void SaveAs()
       {
           if (frmCreateDb.getModelName == string.Empty)
           {
               if (frmCreateDb.ShowDialog() == System.Windows.Forms.DialogResult.OK)
               {
                   try
                   {
                       db.CreateDatabase();
                       MessageBox.Show("New Project is successfully created");
                       svdb = new saveMetabolites(this);
                       svdb = new saveCoMetabolites(this);
                       svdb = new saveKineticEq(this);
                       svdb = new saveKineticPar(this);
                   }
                   catch (Exception ex)
                   {
                       MessageBox.Show(ex.Message);
                   }
               }
           }
           else
           {
               try
               {
                 svdb = new saveMetabolites(this);
                 svdb = new saveCoMetabolites(this);
                 svdb = new saveKineticEq(this);
                 svdb = new saveKineticPar(this);
               }
               catch (Exception ex)
               {
                   MessageBox.Show(ex.Message);
               }
           }
       }

推荐答案

你显然希望我们猜猜这是哪一行错误发生在?



错误很容易诊断。在调试器下运行您的应用程序。当错误出现时,它将直接指向抛出它的行。使用调试器查看该行上的所有变量,并找到返回 null 的变量。您的代码假设在没有返回任何对象时,某些代码返回了一个对象。然后,您尝试在该对象上调用方法(null)或在其上获取/设置属性。由于你不能在一个不存在的对象上做到这一点,你会得到你所看到的错误。



由你来回顾代码和弄清楚为什么你得到一个 null 返回一个对象而不是你期望的那样。
You're apparently expecting us to guess which line this error happens on?

The error is really easy to diagnose. Run your app under the debugger. When the error shows up, it'll be pointing directly at the line that threw it. User the debugger to look at all of the variables on that line and find the one that returns null. Your code is making the assumption that an object was returned by some piece of code when no object was returned. You are then trying to either call a method on that object (null) or get/set a property on it. Since you cannot do that on an object that doesn't exist, you get the error you're seeing.

It's up to you to look backwards through the code and figure out why you're getting a null back for an object instead of what you're expecting.


你没有实例化的命令使用新关键字

试试这个

protected MySqlCommand command = new MySqlCommand()

这可以解决你的问题
u hav not instantiated command with new keyword
Try this
protected MySqlCommand command=new MySqlCommand()
This may solve your issue


这篇关于数据库问题。 object ref未设置为object的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆