1

I have this code which meant to send a user an email notification which generates an error, so I created a label in the main page where user expects to get notified he visits the page then the label text changes to the notification status. so I created label and created a method that changes the label text value to the error message that I need to be shown. when I call the method within the same class it works fine, but when I call it from another class it throws "System.NullReferenceException: Object reference not set to an instance of an object." error.

Here is the notify class:

using OICEVENTS.home;
using System;
using System.Net.Mail;

namespace OICEVENTS.inc.func
{
    public class Notify
    {
        public void EmailNotify()
        {
            string _fromMail = "111@111.com";
            string _toMail = "111@111.com";
            string _subjectMail = "Test mail notification";
            string _bodyMail = "test message";
            string _smtpHost = "111.com";
            string _smtpPwd = "111";
            string _smtpUsr = "111";
            int _smtpPort = 25;
            bool _smtpSSL = true;
            bool _smtpHTML = true;

            SmtpClient smtpClient = new SmtpClient(_smtpHost, _smtpPort);

            smtpClient.Credentials = new System.Net.NetworkCredential(_smtpUsr, _smtpPwd);
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.EnableSsl = _smtpSSL;

            MailMessage mailMessage = new MailMessage(_fromMail, _toMail);
            mailMessage.Subject = _subjectMail;
            mailMessage.Body = _bodyMail;
            mailMessage.IsBodyHtml = _smtpHTML;

            evpg _evpg = new evpg();
            try
            {
                smtpClient.Send(mailMessage);
                _evpg.lblMsg("Message Successfuly Sent ..."); //I always get the error here
            }
            catch (Exception ex)
            {
                _evpg.lblMsg(ex.ToString()); //and here
            }
        }
    }
}

and here is where I call the method from another class and assign some text to it:

using OICEVENTS.inc.func;
using System;

namespace OICEVENTS.home
{
    public partial class evpg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ////initiate data access class
            DbAccess DbAccess = new DbAccess();

            //Fill repeater
            evRepList.DataSource = DbAccess.GetLatestEventsList();
            evRepList.DataBind();

            Notify _sendNotification = new Notify();
            _sendNotification.EmailNotify();
        }

        public void lblMsg(string _msg)
        {
            if(!String.IsNullOrWhiteSpace(_msg))
            {
                evFrmMsg.Text = _msg ;
            }
        }
    }
}

1 Answers1

1

I think you are getting a NullReferenceException because evFrmMsg isn't instantiated, try to change your method to:

public void lblMsg(string _msg)
{
    Label evFrmMsg = new Label();
    if(!String.IsNullOrWhiteSpace(_msg))
    {
        evFrmMsg.Text = _msg ;
    }
    ControlContainingLabel.Controls.Add(evFrmMsg);
}

ControlContainingLabel is the control containing your label.

Slaven Tojic
  • 2,760
  • 2
  • 11
  • 30
  • Still not working, seems that the problem occurs only when I call the method from another class I don't know why ... confusing !! – Ameer Alshamali Apr 07 '18 at 19:41