-1

I have a custom class named Component. Said class gets populated with 4 attributes. I then try to add this class to a List and in this step of the process I get the Object reference not set error. In debugging it I have pines the issue in the add function. Note that I do understand the error just cant pin what is wrong in this specific situation I don't think it is a duplicated question but I do accept link to any question that solves this situation.

         List<Components.Component> partList = new List<Components.Component>();

        firstUsages = false;

        Components.Component targetComponent = new Components.Component();
        targetComponent.A = 5;
        targetComponent.B = 8;
        targetComponent.C = 10;
        targetComponent.TypeComponent = "Teste";

        partList.Add(targetComponent);

Custom Class:

    class Component
{
    double a, b, c;
    string typeComponent;

    public double A { get => a; set => a = value; }
    public double B { get => b; set => b = value; }
    public double C { get => c; set => c = value; }
    public string TypeComponent { get => typeComponent; set => typeComponent = value; }


}
PeterKima
  • 43
  • 1
  • 10
  • 3
    Did you shorten the code for posting? From the snippet, I would say that exception should be impossible. – Fildor Dec 13 '17 at 12:30
  • I did but the rest of the code has no interaction whatsoever with the code presented in the snippet. – PeterKima Dec 13 '17 at 12:55
  • I am afraid we cannot help you. The snippet you posted *cannot* reproduce the exception you mention. – Fildor Dec 13 '17 at 13:01
  • yhea reduced it to a Void function though and the error is still there so there really isn't more code related for me to post. Thanks anyway to everyone trying to help. – PeterKima Dec 13 '17 at 13:11

2 Answers2

0

use it like this:

 partList.Add(new Components.Component 
 {
    A = 5;
    B = 8;
    C = 10;
    TypeComponent = "Teste";
 });
Barr J
  • 10,636
  • 1
  • 28
  • 46
  • You are right, edited the code issue. And unless the Components.Component is a null object, it should work just fine. – Barr J Dec 13 '17 at 12:54
  • 1
    Yes, it should work, but so should the OP's code. They are equivalent. Your answer provides no added value. – Sefe Dec 13 '17 at 12:55
  • 1
    I don't get what differs. It doesn't solve the issue – PeterKima Dec 13 '17 at 12:56
  • 1
    Since List accepts NULL values, it only could be the `partList` itself that is causing the NRE. So this won't change anything. And from OP's code it is not reproducable. – Fildor Dec 13 '17 at 12:59
  • Added the partList class code. Never have I had this error in this situation – PeterKima Dec 13 '17 at 13:04
  • 1
    Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762) would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made. – Melebius Dec 13 '17 at 14:10
0

Try doing this,

partList.Add(new Components.Component{targetComponent});

While using the Add method, you will have to explicitly initialize it the way I have mentioned above.

Monish Koyott
  • 374
  • 1
  • 4
  • 20
  • Using this syntax, is not smart, because you create a new instance of the class, then appending the new instance inside the new instance of the class. This is a double work just for nothing and you create a double reference for the same class, better to add the values right into the list :) – Barr J Dec 13 '17 at 12:41
  • This syntax makes no sense. You pass the previously created instance in the initializer of a new object without assigning it anywhere. It simply won't compile. – Sefe Dec 13 '17 at 12:54