Столкнулся с ошибкой: Ссылка на объект не указывает на экземпляр объекта. Гугл не дает ответа
Я пытаюсь реализовать паттерн проектирования MVP. В С# новичок и многие вещи не знаю. Суть задания: У меня есть форма с тремя кнопками, их я реализовал и все работает. Теперь мне понадобилось добавить форму "Идентификация", которая будет появляться перед запуском первой формы. На форме "идентификация" есть кнопка, по нажатию на которую срабатывает событие, но оно не срабатывает, так как возникает ошибка "Ссылка на объект не указывает на экземпляр объекта". Ниже прикреплю файлы слоя View (формы "идентификация") и presenter. Ошибка возникает на моменте проверки PictureBoxClickDown==null (я закомментировал, так как эта ошибка обходилась.
View
using System;
using System.Windows.Forms;
namespace HumanAsSystem
{
public interface IIdentificationForm
{
event EventHandler PictureBoxClickDown;
string UserName { get; }
}
public partial class IdentificationForm : Form, IIdentificationForm
{
public event EventHandler PictureBoxClickDown;
public IdentificationForm()
{
InitializeComponent();
TestButton.Click += new EventHandler(PictureBoxClick);
}
private void PictureBoxClick (object sender, EventArgs e)
{
// if (PictureBoxClickDown != null)
// {
PictureBoxClickDown(this, EventArgs.Empty);
// }
}
public string UserName
{
get { return Name_User.Text; }
}
private void IdentificationForm_Load(object sender, EventArgs e)
{
}
}
}
Presenter
using System;
using HumanAsSystemTask1;
using HumanAsSystemTask2;
using HumanAsSystemTask3;
using HumanAsSystemBL;
using System.Windows.Forms;
//using IdentificationForm;
namespace HumanAsSystem
{
class MainPresenter
{
private readonly IMainForms _mainform;
private readonly MS.IMessageService _message_service;
private readonly IIdentificationForm _identification;
public MainPresenter (IMainForms mainform, MS.IMessageService message_service, IIdentificationForm identification)
{
_mainform = mainform;
_message_service = message_service;
_identification = identification;
_mainform.ClickTask1 += new EventHandler(_mainform_ClickTask1);
_mainform.ClickTask2 += new EventHandler(_mainform_ClickTask2);
_mainform.ClickTask3 += new EventHandler(_mainform_ClickTask3);
_mainform.MainFormLoad += new EventHandler(_mainform_load);
_identification.PictureBoxClickDown += new EventHandler(_identification_Click);
}
void _mainform_load (object sender, EventArgs e)
{
IdentificationForm identification = new IdentificationForm();
identification.ShowDialog();
}
void _mainform_ClickTask1 (object sender, EventArgs e)
{
Task1Form form1 = new Task1Form();
form1.ShowDialog();
}
void _mainform_ClickTask2(object sender, EventArgs e)
{
Task2Form form2 = new Task2Form();
form2.ShowDialog();
}
void _mainform_ClickTask3(object sender, EventArgs e)
{
Task3Form form3 = new Task3Form();
form3.ShowDialog();
}
void _identification_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(_identification.UserName) == false)
{
MessageBox.Show("Вы не указали имя пользователя");
}
else
{
MessageBox.Show("Вы не указали имя пользователя");
}
}
}
}
На всякий случай прикреплю IdentificationForm.Designer
namespace HumanAsSystem
{
partial class IdentificationForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.TestButton = new System.Windows.Forms.Button();
this.Name_User = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// TestButton
//
this.TestButton.Location = new System.Drawing.Point(141, 59);
this.TestButton.Name = "TestButton";
this.TestButton.Size = new System.Drawing.Size(75, 23);
this.TestButton.TabIndex = 0;
this.TestButton.Text = "button1";
this.TestButton.UseVisualStyleBackColor = true;
// this.TestButton.Click += new System.EventHandler(this.PictureBoxClick);
//
// Name_User
//
this.Name_User.Location = new System.Drawing.Point(51, 24);
this.Name_User.Name = "Name_User";
this.Name_User.Size = new System.Drawing.Size(260, 20);
this.Name_User.TabIndex = 1;
this.Name_User.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// IdentificationForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(207)))), ((int)(((byte)(213)))), ((int)(((byte)(225)))));
this.ClientSize = new System.Drawing.Size(374, 139);
this.Controls.Add(this.Name_User);
this.Controls.Add(this.TestButton);
this.Name = "IdentificationForm";
this.Text = "Form1";
this.Load += new System.EventHandler(this.IdentificationForm_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button TestButton;
private System.Windows.Forms.TextBox Name_User;
}
}
Заранее спасибо! Гугл не дал мне ответа, только больше закапался в терминах.