【问题标题】:Cannot locate the error causing 'Object reference not set to an instance of an object'找不到导致“对象引用未设置为对象实例”的错误
【发布时间】:2013-12-07 01:15:23
【问题描述】:

我正在尝试通过复选框发送邮件,我已经完成了,但是在这里我希望管理员单击复选框并按下按钮,然后管理员从转发器获取文档名称和状态的值,然后将邮件发送给用户

就像管理员以任何电子邮件 ID 发送邮件,然后当用户收到邮件文档名称时显示如下:abc 状态:拒绝

DocID  DocName  Uplaodedfile  UserEmail          DocType  DepType status
1      ABC      def.pdf       abcdef@gmail.com   pdf      hr      reject
2      hr       hrdoc.pdf     johkety@gmail.com  pdf      hr      approve

这是电子邮件按钮代码

protected void btnSendMail_Click(object sender, EventArgs e)
{
    string connStr = ConfigurationManager.ConnectionStrings["mydms"].ConnectionString;
    SqlConnection mySQLconnection = new SqlConnection(connStr);
    string empId = string.Empty;
    DataTable dt = new DataTable();

    try
    {
        mySQLconnection.Open();

        for (int i = 0; i < Repeateremail.Items.Count; i++)
        {
            CheckBox checkboc = ((CheckBox)Repeateremail.Items[i].FindControl("chkSelect"));

            if (checkboc != null)
            {
                if (checkboc.Checked == true)
                {
                    //get Current EMAIL_ID from the DataKey
                    string emailId = (Label)Repeateremail.Items[i].FindControl("lbl_email")).Text;
                    string DocName = ((Label)Repeateremail.Items[i].FindControl("DocName")).Text;
                    string Status =  ((Label)Repeateremail.Items[i].FindControl("Status")).Text;

                    //write code to send mail
                    SendEmailUsingGmail(emailId,DocName,Status);
                    dt.Clear();
                    dt.Dispose();
                }
                else if (checkboc.Checked == false)
                {
                }
            }
        }               
    }
    catch (Exception ex)
    {
       emailsent.Text="Failed";
    }
    finally
    {
      empId = string.Empty;
    }
}

private void SendEmailUsingGmail(string toEmailAddress,string DocName,string Status)
{
    try
    {
        SmtpClient smtp = new SmtpClient();
        smtp.Credentials = new NetworkCredential("johmm@gmail.com", "12234");
        smtp.Port = 587;
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        MailMessage message = new MailMessage();
        message.From = new MailAddress("johmm@gmail.com");
        message.To.Add(toEmailAddress);
        message.To.Add(DocName);
        message.To.Add(Status);
        message.Subject = "Write your email subject here";
        message.Body = "write the content of the email here";
        smtp.Send(message);
    }
    catch (Exception ex)
    {
        Response.Write("Error occured: " + ex.Message.ToString());
    }
}

但它显示错误

在这一行

catch (Exception ex)
{
   emailsent.Text="Failed";
}

`错误:对象引用未设置为对象的实例

【问题讨论】:

  • 你能输出ex中的内容吗,因为它会给你一个行号。
  • 调试并查看哪一行抛出错误。可能是配置中缺少连接
  • 您也不需要dt.Clear()dt.Dispose(),因为您没有使用它。它也可能是演员之一。
  • 最有可能的错误是由于以下三行之一: string emailId = ((Label)Repeateremail.Items[i].FindControl("lbl_email")).Text;字符串 DocName = ((Label)Repeateremail.Items[i].FindControl("DocName")).Text; string Status = ((Label)Repeateremail.Items[i].FindControl("Status")).Text;
  • 当我只发送电子邮件然后电子邮件发送成功但是当我编码这个 ((Label)Repeateremail.Items[i].FindControl("DocName")).Text; string Status = ((Label)Repeateremail.Items[i].FindControl("Status")).Text;还要发送文档名称和状态,它会显示错误

标签: asp.net email nullreferenceexception


【解决方案1】:

可能是你的 for 循环没有找到控件。使用 'as' 关键字转换为所需的类型。它不会抛出异常。相反,它会尝试它,如果不可能,则返回null。尝试使用这种格式:

for (int i = 0; i < Repeateremail.Items.Count; i++)
{
    CheckBox checkbox = Repeateremail.Items[i].FindControl("chkSelect")) as CheckBox;

    if (checkbox != null) 
    {
        if (checkbox.Checked) 
        {
            Label emailLabel = Repeateremail.Items[i].FindControl("lbl_email")) as Label;
            Label docNameLabel = Repeateremail.Items[i].FindControl("DocName")) as Label;
            Label statusLabel = Repeateremail.Items[i].FindControl("Status")) as Label;
            //get Current EMAIL_ID from the DataKey
            string emailId = emailLabel.Text;
            string DocName = docNameLabel.Text;
            string Status =  statusLabel.Text;

            //write code to send mail
            SendEmailUsingGmail(emailId,DocName,Status);
            dt.Clear();
            dt.Dispose();
        }
        else
        {
        }
    }
} 

然后使用调试器单步执行并检查所有这些标签不为空。您很快就会知道,因为.Text 会失败,但调试器应该会提醒您。

【讨论】:

  • 当我在此代码字符串中设置断点时尝试您的代码 emailId = emailLabel.Text;字符串 DocName = docNameLabel.Text;字符串状态 = statusLabel.Text;它向我显示 emailId 像 abcdef@gmail.com 但在 DocName 和状态中它向我显示这样的“”
  • 在此代码字符串中 emailId = emailLabel.Text;字符串 DocName = docNameLabel.Text;字符串状态 = statusLabel.Text;它向我显示 emailId 像 abcdef@gmail.com 但在 DocName 和状态中它向我显示这样的“”
  • 好的。所以那不是你问题的根源。
猜你喜欢
  • 2012-07-06
  • 2015-09-09
  • 2016-06-10
  • 2011-07-09
  • 2012-08-31
  • 2011-11-21
  • 2012-10-01
  • 1970-01-01
相关资源
最近更新 更多