8

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());
2
  • 2
    Check inner exception for exact exception and post it here. Commented Dec 14, 2013 at 7:51
  • 1
    "Object reference not set to an instance of an object" Commented Dec 14, 2013 at 7:59

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

Comments

7

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;

4 Comments

Glad to help Affuu. On a side note, try to strict to naming conventions provided by Microsoft for naming your class. It can be found here.
oh. Thats great. In Java this would not happen. Thanks for your solution. Java would implicitely create object for each element of the array. Pretty interessting!
@ChristianSchack That is not true. If you create an array of a reference type in Java, all elements are null unless you initialize them.
@Clemens Thx for your advise. Yeah i think you are right. So this problem leads also in Java to an InvoactionTargetException which indeed is wrong becaus the main reason is a NullPointerException because there is now instance created on which the TargetInvocation can be processed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.