Reflection.TargetInvocationException

37,824

Solution 1

You are creating an array but all items added in array are still null.

Initialize them first and then only you can access it. Problem is here -

Random randi = new Random();
carroms[] mycarroms = new carroms[5];
mycarroms[0].carromHeight = 100;  <-- mycarroms[0] will be null

It should be -

Random randi = new Random();
carroms[] mycarroms = new carroms[5];
mycarroms[0] = new carroms();
mycarroms[0].carromHeight = 100;

Or you can use array initializer to initialize it -

Random randi = new Random();
carroms[] mycarroms = new carroms[5]
   {new carroms(), new carroms(), new carroms(), new carroms(), new carroms()};
mycarroms[0].carromHeight = 100;

Solution 2

Want to add something, Don't get intimidated with TargetInvocationException as it does not serve too much of information. You should See Inner Exception to get the root cause. InnerException could be of type AggregateException, in that case you need to go further down to get all the exception details.

Share:
37,824
Affuu
Author by

Affuu

Updated on July 09, 2022

Comments

  • Affuu
    Affuu almost 2 years

    I have a class named carroms. When I create its object, there is no error. But when I create an array of carroms then, this exception is thrown:

    An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in PresentationFramework.dll

    Additional information: Exception has been thrown by the target of an invocation.

    My code for the carroms class:

    class carroms
    {
    
        private bool player;
    
        public bool checkPlayer
        {
            get { return player; }
            set { player = value; }
        }
    
        private Point center;
    
        public Point carromCenter
        {
            get { return center; }
            set { center = value; }
        }
    
        private Point[] points;
    
        public Point[] carromPoints
        {
            get { return points; }
            set { points = value; }
        }
    
        private double width;
    
        public double carromWidth
        {
            get { return width; }
            set { width = value;
            }
        }
    
        private double height;
    
        public double carromHeight
        {
            get { return height; }
            set { height = value; }
        }
    
        public carroms()
        {
            points = new Point[370];
        }
    
        public Ellipse draw()
        {
            Ellipse myellipse = new Ellipse();
            myellipse.Height = carromHeight;
            myellipse.Width = carromWidth;
            if (checkPlayer == true)
            {
                myellipse.Fill = Brushes.Black;
            }
            else
            {
                myellipse.Fill = Brushes.Beige;
            }
            return myellipse;
        }
    }
    

    And my code for creating the object:

    Random randi = new Random();
    carroms[] mycarroms = new carroms[5];
    mycarroms[0].carromHeight = 100;
    mycarroms[0].carromWidth = 100;
    mycanvas.Children.Add(mycarroms[0].draw());