这是个经常遇到的问题,我在winform中做了个用户登录的界面,输入用户名和密码后登录会提示“未将对象引用设置到对象的实例”,详细如下:未处理 System.NullReferenceException
  Message="未将对象引用设置到对象的实例。"
  Source="CRM_Login"
  StackTrace:
       在 lengku48.Form1.button1_Click(Object sender, EventArgs e) 位置 E:\冷库48\Form1.cs:行号 112
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.PerformClick()
       在 System.Windows.Forms.Form.ProcessDialogKey(Keys keyData)
       在 System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
       在 System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
       在 System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData)
       在 System.Windows.Forms.Control.PreProcessMessage(Message& msg)
       在 System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
       在 System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
       在 System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 lengku48.Program.Main() 位置 E:\冷库48\Program.cs:行号 17
       在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException:
出错位置:
     private void button1_Click(object sender, EventArgs e)
        {
            SqlDataReader sdr = null;
            if (String.IsNullOrEmpty(txt_LoginName.Text.Trim()))
            {
                MessageBox.Show("登录用户不许为空!", "软件提示");
                txt_LoginName.Focus();
                return;
            }
            if (String.IsNullOrEmpty(txt_LoginPwd.Text))
            {
                MessageBox.Show("登录密码不许为空!", "软件提示");
                txt_LoginPwd.Focus();
                return;
            }
            //用户编码不重复
            string strSql = "select * from Operator where OperatorCode = '" + txt_LoginName.Text.Trim() + "'";
            try
            {
                sdr = dal.GetDataReader(strSql);
                if (!sdr.HasRows)  //若该用户编码无数据记录
                {
                    MessageBox.Show("登录用户不正确!", "软件提示");
                    txt_LoginName.Focus();
                }
                else
                {
                    sdr.Read(); //读取唯一的一行记录
                    if (!(txt_LoginPwd.Text == sdr["Password"].ToString()))  //若密码不相同
                    {
                       
                        MessageBox.Show("登录密码不正确!", "软件提示");
                        txt_LoginPwd.Focus();
                    }
                    else
                    {
                        GlobalProperty.OperatorCode = sdr["OperatorCode"].ToString();
                        GlobalProperty.OperatorName = sdr["OperatorName"].ToString();
                        GlobalProperty.Password = sdr["Password"].ToString();
                        GlobalProperty.IsFlag = sdr["IsFlag"].ToString();
                        //this.Hide();
                        timer1.Start();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
            }
            finally
            {
                sdr.Close();//出错代码
            }        }
希望大家能帮我分析一下,是try...catch...finally有问题还是数据库的事,先谢过了。

解决方案 »

  1.   

    应该是数据库的问题,sdr = dal.GetDataReader(strSql)返回了null
      

  2.   

    要让它不报错加上判断
    if(sdr != null)
    {
        sdr.Close();
    }
    应该就可以了。
    但是最好看看dal.GetDataReader(strSql)为什么会返回null。
      

  3.   

    程序不往下运行了,呜呜,怎么看dal.GetDataReader(strSql)为什么会返回null呀?
      

  4.   

    在dal.GetDataReader(strSql)这里打个断点,调试按F11进去,把GetDataReader方法走一遍
      

  5.   

    我的程序在Form1.cs的
     private void timer1_Tick(object sender, EventArgs e)
            {
                    this.Opacity += dou;
                    if (this.Opacity == 1)
                    {
                        timer1.Stop();
                        dou = -0.05;
                        timer2.Start();
                    }else if (this.Opacity == 0)
                    {
                        timer1.Stop();
                        Form2 form = new Form2();
                        show.Get_Show(form);
                    }
            }
    处循环不出来了,不知道是什么原因
      

  6.   

    在catch处出现异常:
     catch (Exception e)
                {
                    throw e;
                }
    捕捉到SqlException:
    在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错)
    这会是什么问题引起的啊?不知道您能不能指点一下迷津呀?
      

  7.   

    防火墙关的,连接字符串应该是对的string strConn = "Server =. " + strServer + ";Database=lengku;User id=sa" + strUserID + ";PWD=sa" + strPwd;
    我昨天也搜过那个异常,不过没什么收获,我再查查看吧,谢谢你啦!
      

  8.   

    不对吧,就看这个"Server =. " + strServer,"."是本机,"." + strServer是什么意思?
    应该是这样吧
    string strConn = "Server = " + strServer + ";Database=lengku;User id=" + strUserID + ";PWD=" + strPwd;
      

  9.   

    还是字符串的问题,谢谢ParanoidKing啦!
      

  10.   

    应该是连接字符串的问题。你建一个空文件,把扩展名改成.udl,双击打开,把参数填一下,然后测试连接,看看是否能联通。联通之后用记事本打开udl文件,看看连接字符串跟你代码那个有什么不同,改一下。
      

  11.   

    我建了一个ini文件,在那里写
    [DataBase]
    Server=CY-63FBE3FF6AE5\SQLEXPRESS
    UserID=sa
    Pwd=sa
    这样调用就好用了