-3

This is my property class:

 class Actions
 {
     public string[] Style { get; set; }
 }

and this is my main method:

Actions action = new Actions();

List<string> list = new List<string>();
list.Add("one");
list.Add("two");

foreach (var item in list)
{
    for (int i = 0; i < action.Style.Length; i++)
    {
        action.Style[i] = item.ToString();
        Console.WriteLine(action.Style[i]);
    }                  
}

How do I fill the property with list items?

This gives me a exception:
"object reference not set to an instance of an object".

David
  • 3,810
  • 2
  • 31
  • 52
Arman
  • 45
  • 7
  • 7
    `Style` has not been instantiated. – Matt Rowland Nov 16 '16 at 17:00
  • 1
    Not sure if Actions is the most appropriate name for your class. It should really be a singular word. Obviously Action is not permitted, so you might want to think of something more descriptive. – openshac Nov 16 '16 at 17:06
  • May I'm wrong, but with the second list item you will overwrite the content of your style... Is this intended? And you iterate over your array, but what size has this array? Code looks confusing. – dannyyy Nov 16 '16 at 17:12
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Owen Pauling Nov 16 '16 at 17:16
  • how to see the output of Style property? – Arman Nov 16 '16 at 17:24
  • @openshac it's just an example – Arman Nov 16 '16 at 17:26

3 Answers3

1

There is no need to add your items one by one, you could just use the ToArray() method of your list like so:

List<string> list = new List<string>();
list.Add("one");
list.Add("two");

Actions action = new Actions {
    Style = list.ToArray()
};
paul
  • 20,643
  • 1
  • 48
  • 53
0

You must create an instance of the Style property

List<string> list = new List<string>();
list.Add("one");
list.Add("two");
Actions action = new Actions();
action.Style=new string[list.Count];

foreach (var item in list)
{
    for (int i = 0; i < action.Style.Length; i++)
    {
        action.Style[i] = item.ToString();
        Console.WriteLine(action.Style[i]);
    }                  
}
David
  • 3,810
  • 2
  • 31
  • 52
0

As has already been pointed out, Style is always null, given the code you have shared. @Eldeniz and @paul have shared different ways to fix that. Obviously, your sample code is just a sample fragment, so here are 2 other options you could consider if the previous two don't work for whatever reason (I'm just free-handing this, please excuse any typos).

1) You can have your Actions class always return a not-null object

class Actions
{
    private string[] _style;
    public string[] Style 
    {
        get { return _style ?? new string[0]; }
        set { _style = value; }
    }
}

Note that this will allow you to always see the output of the style property as requested, assuming an empty array and null are, for your purposes, the same thing.

2) You can make your loop tolerant to null values

foreach (var item in list)
{
    for (int i = 0; i < action?.Style.Length ?? 0; i++)
    {
        action.Style[i] = item.ToString();
        Console.WriteLine(action.Style[i]);
    }                  
}

Finally, just as a tip, if you have your debugger attached and you are stepping through your code, Visual Studio will help you pinpoint these sorts of errors pretty easily. Take the time to become friends with your debugger. If it gives you an error you don't understand, do a quick web search. Your future self will thank you.

David
  • 3,810
  • 2
  • 31
  • 52