Question

I'm studying design patterns right now, I'm fairly new to this model-view-presenter, although I have already experience in asp.net mvc I'm trying to do an implementation of mvp in winforms.

The string in the textbox will be sorted with an algorithm based on the combobox. Right now when I click the button it throws a null reference exception

Here is the UI:enter image description here

Here are my classes and codes:

    class FormPresenter
        {
            private ISortingView _view;
            private string _algorithm;
            private StringToSortModel sortMe = new StringToSortModel();

            public FormPresenter(ISortingView view)
            {
                _view = view;
                _view.sortTheString += view_sortString;
                sortMe.sortThis = view.stringToSort;
                _algorithm = _view.algorithm;
                //Algorithm = view.stringToSort;
                //sortingform.sortTheString += (obj
            }

            private void view_sortString(object sender, EventArgs e)
            {

                SortContext context = new SortContext();
                _view.sortedText = context.Sort(sortMe.sortThis.ToCharArray());


            }

        }


 interface ISortingView
    {
        event EventHandler sortTheString;
        string stringToSort { get; }
        string algorithm { get; }
        string sortedText { get; set; }

    }


     public partial class SortingForm : Form, ISortingView
        {
            public SortingForm()
            {
                InitializeComponent();
                comboBox1.Items.Add("Bubble Sort");
                comboBox1.Items.Add("Insertion Sort");
                comboBox1.SelectedItem = "Bubble Sort";
                textBox1.Text = "Emiri";
            }


            public event EventHandler sortTheString;
            public string algorithm { get { return comboBox1.SelectedItem.ToString(); } }
            public string stringToSort { get { return textBox1.Text; } }
            public string sortedText { get { return label2.Text; } set { label2.Text = value; } }




            private void Form1_Load(object sender, EventArgs e)
            {

            }


            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {

            }

            private void button1_Click(object sender, EventArgs e)
            {
                //char[] x = textBox1.Text.ToCharArray();
                //SortContext con = new SortContext();
                //con.SetSortStrategy(new InsertionSort());
                //label2.Text = con.Sort(x);
                //if(sortString != null)
                //{

                //this prodcues a null exception error
                sortTheString(sender, e);


                //}



            }

    static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                var mainForm = new SortingForm();
                var presenter = new FormPresenter(mainForm);
                Application.Run(new SortingForm());


            }
        }

I have not included the codes for the model and the classes the contains the sorting functions to keep this post short. The problem I have is that when button is clicked it throws a null reference exception error, something that I have been stuck on for hours already.

Sir/Ma'am your answers would be of great help. Thank you++

Was it helpful?

Solution

Your null is coming from this line

sortTheString(sender, e);

because you are not using the same form instance in your Presenter. Change to this in your main...

Application.Run(mainForm);

The event handler does not have any subscribers (because of the Application.Run(new SortingForm()); C# will treat that as null rather than an empty subscriber list.

OTHER TIPS

ISortingView mainForm = new SortingForm();
var presenter = new FormPresenter(mainForm);
Application.Run(mainForm as Form);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top