HELP对象引用未设置为对象的瞬间 [英] HELP Object refrence not set to an instant of an object

查看:139
本文介绍了HELP对象引用未设置为对象的瞬间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我似乎不明白为什么我会不断收到此错误
Object refrence not set to an instant of an object

和即时通讯在connection.Open();
上获取它 但我似乎不明白为什么


这是使用
的代码

Okay i dont seem to understand why i keep getting this error
Object refrence not set to an instant of an object

and im Getting it on connection.Open();
but i dont seem to understand why


here is the code im using

public MySqlConnection connection;
       public void DBConnect()
       {
           Initialize();
       }
       private void Initialize()
       {
           string datasource = "";
           string initialcatalog = "";
           string uid = "";
           string pwd = "";
           string connectionstring;
           connectionstring = "SERVER='" + datasource + "';DATABASE='" + initialcatalog + "';UID='" + uid + "';PASSWORD='" + pwd + "'";
           connection = new MySqlConnection(connectionstring);
       }
       private bool OpenConnection()
       {
           try
           {
               connection.Open();
               return true;
           }
           catch (Exception ex)
           {
               switch (ex.Number)
               {
                   case 0:
                       System.Windows.Forms.MessageBox.Show("Cannot Connect to server. Contact administrator");
                       break;
                   case 1045:
                       System.Windows.Forms.MessageBox.Show("Invalid Username/Password for mysql, Please try again");
                       break;
               }
               return false;
           }
       }

推荐答案

连接似乎为空.


似乎从未运行过Initialise -在呼叫OpenConnection之前先呼叫DBConnect还是Initialize?尝试在该方法中放置一个断点,以查看在运行程序时是否调用了该断点.

当然,我看不到该类的其余部分,但是我假设您的意思是让DBConnect成为构造函数.如果是这种情况,则它前面不应带有void关键字,并且您还需要确保其名称与它所在的类的名称相同.
It looks like Initialise is never being run - are you calling DBConnect or Initialize before calling OpenConnection? Try putting a breakpoint in that method to see if it''s called when you run your program.

Of course, I can''t see the rest of the class, but I assume that you mean for DBConnect to be the constructor. If that''s the case, it shouldn''t have the void keyword in front of it, and you also need to make sure it has the same name as the class in which it resides.


尝试将代码的这一部分更改为此:

connectionstring = "SERVER=" + datasource + ";DATABASE=" + initialcatalog + ";UID=" + uid + ";PWD=" + pwd;
Try to change this part of the code to this one:

connectionstring = "SERVER=" + datasource + ";DATABASE=" + initialcatalog + ";UID=" + uid + ";PWD=" + pwd;


这篇关于HELP对象引用未设置为对象的瞬间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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