TargetInvocationException i MvP

0

InnerException: Check to determine if the object is null before calling the method.
InnerException: Use the "new" keyword to create an object instance.

Mam taki problem ze otrzymuje taki exception gdy z widoku staram sie wywolac metode prezentera. Po zrobieniu if'a ktory sprawdza czy prezenter jest null i jesli jest null to nie wywoluje metody, problem zostal naprawiony. Ale tak imho nie powinno byc bo prezenter niepowinien być null... Tak mi sie przynajmniej wydaje, zwlaszcza ze zrobilem MvP tak samo jak robilem go zawsze i wczesniej działał :\

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;

namespace Prezenter
{
    public class CPrezenter
    {
        private IView _view;
        private CData _data;

        public CPrezenter(IView view)
            : this(view, new CData())
        { }

        public CPrezenter(IView view, CData data)
        {
            _view = view;
            _data = data;
        }

        public void DoTheCount(int FirstChoice, double FirstAmount, int SecondChoice)
        {

        }

        public string Dawaj()
        {
            return "lelele";
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using Prezenter;

namespace View
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, IView
    {
        private CPrezenter prezenter;
        public MainWindow()
        {

            InitializeComponent();
            prezenter = new CPrezenter(this);
            
        }

        private void _tb_amount_left_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            
           // Regex Check1 = new Regex(@"^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?");  
            Regex Check1 = new Regex(@"^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?[\.]{0,1}?");      
            string nameCheck = e.Text;           
            if (!Check1.IsMatch(nameCheck))
            {
                e.Handled = true;
            }
        }

        private void _tb_amount_left_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.V && Keyboard.IsKeyDown(Key.LeftCtrl))
            {
                if(Clipboard.ContainsText())
                {
                    Regex Check1 = new Regex(@"^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?");
                   if(!Check1.IsMatch(Clipboard.GetText()))
                   {
                       Clipboard.Clear();
                   }
                }
               
                
            }
            if (e.Key == Key.Space)
            {
                e.Handled = true;                
            }
        }

        private void _tb_amount_right_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            // Regex Check1 = new Regex(@"^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?");  
            Regex Check1 = new Regex(@"^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?[\.]{0,1}?");
            string nameCheck = e.Text;
            if (!Check1.IsMatch(nameCheck))
            {
                e.Handled = true;
            }
        }

        private void _tb_amount_right_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.V && Keyboard.IsKeyDown(Key.LeftCtrl))
            {
                if (Clipboard.ContainsText())
                {
                    Regex Check1 = new Regex(@"^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?");
                    if (!Check1.IsMatch(Clipboard.GetText()))
                    {
                        Clipboard.Clear();
                    }
                }


            }
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
        }

        private void _tb_amount_left_TextChanged(object sender, TextChangedEventArgs e)
        {
            int fc, sc;
            double fa;
            try
            {
                fc = (int)_cb_left.SelectedIndex;
                sc = (int)_cb_right.SelectedIndex;
                fa = Convert.ToDouble(_tb_amount_left.Text);
               // _prezenter.DoTheCount(fc, fa, sc);
                
                    MessageBox.Show(prezenter.Dawaj());
                

            }
            catch (InvalidCastException ex)
            {
                MessageBox.Show("Wrong orbs amout.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
                
        }

        private void _tb_amount_right_TextChanged(object sender, TextChangedEventArgs e)
        {

        }



        public string LeftAmount
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string RightAmount
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }
}
0

Na pierwszy rzut oka odpala się _tb_amount_left_TextChanged w InitializeComponent(). Spróbuj w konstruktorze przestawić miejscami linie:

 
        public MainWindow()
        {
            prezenter = new CPrezenter(this);
            InitializeComponent();
        }

Tutaj ktoś miał podobny problem:
http://stackoverflow.com/questions/2518231/wpf-getting-control-null-reference-during-initializecomponent

0

A miejsce wyrzucenia wyjątku? StackTrace?

1 użytkowników online, w tym zalogowanych: 0, gości: 1