1

In my user settings for the application, I have a StringDictionary named "websites". A NullReferenceException with the message "Object reference not set to an instance of an object." when it reaches this line: Properties.Settings.Default.websites.Add(nameBox.Text, urlBox.Text);, but I can't figure out why. The VS debugger shows that nameBox.Text and urlBox.Text are the right values.

Edit:

I've determined that it's null by default, so on Form.Load, I added

if (Properties.Settings.Default.websites == null) {
            MessageBox.Show("property is null");
            Properties.Settings.Default.websites = new StringDictionary();
}

but now the settings aren't saving.

airplaneman19
  • 1,099
  • 5
  • 18
  • 32
  • 1
    possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders Jul 23 '12 at 22:25
  • 1
    Is any part of Properties.Settings.Default.websites null? – JG in SD Jul 23 '12 at 22:26
  • @JohnSaunders this question is not duplicate to the one you pointed out because it is specifically about Properties.Settings – HatSoft Jul 23 '12 at 22:34
  • @HatSoft: it is very certainly a duplicate. It's another case of, "you expected a reference to refer to something, but, surprise, it doesn't". – John Saunders Jul 23 '12 at 22:41
  • possibly related... http://stackoverflow.com/questions/424010/stringdictionary-not-saving-as-user-setting – pilotcam Jul 23 '12 at 22:46

1 Answers1

0

Looks like your StringDictionary websites Scope is set to Application which cannot be modified at runtime. You will have change the scope to user so it can be changed at runtime per user

Taken from MSDN

Application-scoped settings can be used for information such as a URL for a Web service or a database connection string. These values are associated with the application. Therefore, users cannot change them at run time.

Edit: To save the settings you will have to use Properties.Settings.Default.Save();

HatSoft
  • 10,683
  • 3
  • 25
  • 43