user1327064
user1327064

Reputation: 4337

Why am i getting NullReferenceException?

I have an interface

public interface IConfig
{
    string name { get; set; }
    string address { get; set; }
    int phone { get; set; }

    List<string> children { get; set; }
}

Here is my config file that has only three app settings not four like i have in interface.

<add key="name" value="abc" />
<add key="address" value="100 Norin Street" />
<add key="phone" value="709-111-111111" />

Now on startup I am using DictionaryAdapterFactory to fillin the app.config file values. I am successfully getting values of app.config here.

private readonly IConfig _config;
private readonly DictionaryAdapterFactory _factory;
_factory = new DictionaryAdapterFactory();
_config = _config.GetAdapter<IConfig>(ConfigurationManager.AppSettings);

now at run time I need to fill in the value of children which is List type. But i am getting null exception. Why?

//loop
_config.children.Add(item.values);

What is the problem here?

Upvotes: 0

Views: 146

Answers (3)

nima
nima

Reputation: 1

You Defined 'Phone' as int in interface while phone value in AppSettings is not cast-able to int. change it to string:

  string phone { get; set; }

Upvotes: 0

sarwar026
sarwar026

Reputation: 3821

Will it be like following?

_config.children.AddRange(item.values); 

Of course, the initialization is also required.

_config.children = new List<string>();

Upvotes: 0

Rapha&#235;l Althaus
Rapha&#235;l Althaus

Reputation: 60493

missing somewhere a list initialization ?

_config.children = new List<string>()

Upvotes: 4

Related Questions