Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In the user Settings is set to

Name:DateEventNames
Type:StringCollection
Scope:User
Value: 

This is my code

C#
Properties.Settings.Default.DatesEventNames.Add(EventTextBox.Text);


And this is my error
Object reference not set to an instance of an object

This is weird, because this code should of worked, can some one please explain why this didn't work, and tell me how to fix it?
Posted
Updated 3-Dec-12 22:01pm
v2
Comments
Simon_Whale 4-Dec-12 4:02am    
your wuestion is not clear are you trying to read / write from the app.config or web.config file?
[no name] 4-Dec-12 4:17am    
Im adding it the the stringcollection located in the settings

You have not given much details about where this line of code is written. COnsidering it is used where EventTextBox is already initialized, only other point I can see is initialization of StringCollection

Have you done initialization something like below?

C#
StringCollection myCollection = new StringCollection();


Also, note that name of the property is different as DateEventNames at definition vs. DatesEventNames when you adding. Just check.

Hope that helps
Milind
 
Share this answer
 
v2
Comments
[no name] 4-Dec-12 4:15am    
It is located in

private void button1_Click(object sender, RoutedEventArgs e)
{
}

And no, i don't need to initialization it because it is already setup in the settings as it says in the question
MT_ 4-Dec-12 4:52am    
Also, note that name of the property is different as DateEventNames at definition vs. DatesEventNames when you adding. Just check.
Alan N 4-Dec-12 4:53am    
When you get the NullReferenceException it means the object is not initialised. Don't go looking for other explanations! If you read the setting as soon as your app starts you will see that the value is null. It will be this way unless you have used the designer collection editor to add a few strings.
I would start to have a read of this

C# writing to app.config file[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900