C# - NullReferenceException - В экземпляре объекта не задана ссылка на объект
Формулировка задачи:
Доброго времени суток!
Подскажите пожалуйста в чем может быть проблема?
отлично работает, а на
вылетает NullReferenceException
Листинг программы
- public partial class DateTimePanel : UserControl {
- // vars
- private int curTime;
- public int getTime {
- get { return curTime; }
- }
- public DateTime getDate {
- get { return DateTime.Now; }
- }
- // events
- public event SelectDateEventHandler SelectDate;
- public event TickTimerEventHandler TickTimer;
- protected virtual void OnSelectDate(SelectDateEventArgs e) {
- SelectDate(this, e);
- }
- protected virtual void OnTickTimer(TickTimerEventArgs e) {
- TickTimer(this, e);
- }
- //
- public DateTimePanel() {
- InitializeComponent();
- }
- public bool setTimer(bool en) {
- curTime = -1;
- mainTimer.Enabled = en;
- return mainTimer.Enabled;
- }
- private void calendar_DateSelected(object sender, DateRangeEventArgs e) {
- OnSelectDate(new SelectDateEventArgs(e.Start));
- }
- private void mainTimer_Tick(object sender, EventArgs e) {
- int H, M, time;
- string strtime;
- strtime = DateTime.Now.ToLongTimeString();
- H = Int32.Parse(strtime.Substring(0, 2));
- M = Int32.Parse(strtime.Substring(3, 2));
- time = H * 60 + M;
- if (curTime != time) {
- curTime = time;
- OnTickTimer(new TickTimerEventArgs(time));
- }
- dateLbl.Text = DateTime.Now.ToLongDateString();
- timeLbl.Text = strtime;
- }
- }
- public delegate void SelectDateEventHandler(object sender, SelectDateEventArgs e);
- public delegate void TickTimerEventHandler(object sender, TickTimerEventArgs e);
- public class SelectDateEventArgs : EventArgs {
- private DateTime date;
- public SelectDateEventArgs(DateTime date) {
- this.date = date;
- }
- public DateTime SelectedDate {
- get { return date; }
- }
- }
- public class TickTimerEventArgs : EventArgs {
- private int time;
- public TickTimerEventArgs(int time) {
- this.time = time;
- }
- public int getTime {
- get { return time; }
- }
- }
- }
Листинг программы
- protected virtual void OnSelectDate(SelectDateEventArgs e) {
- SelectDate(this, e);
- }
Листинг программы
- protected virtual void OnTickTimer(TickTimerEventArgs e) {
- TickTimer(this, e);
- }
Решение задачи: «C# - NullReferenceException - В экземпляре объекта не задана ссылка на объект»
textual
Листинг программы
- var tmp = TickTimer;
- if (tmp != null) tmp(this, e);
- var tmp = SelectDate;
- if (tmp != null) tmp(this, e);
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д