0

I am adding a string to a string collection that is located in my program settings.

foreach (string path in prefs.Paths)
{
     Properties.Settings.Default.Paths.Add(path); // <-- returns null if assembly is signed
}

This works without exceptions until I sign the assembly in Solution->Properties->Signing

I created a new strong name key file in that same tab with a password.

When the program is running after signing the assembly I receive this exception:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Program.Properties.Settings.Paths.get returned null.

I removed my 'foreach' loop in an attempt to check if that was the problem by replacing it with:

Properties.Settings.Default.Paths.Add("test string");

The code still works when the assembly is unsigned but throws an exception when it is signed.

I believe that I am obviously missing something that happens when signing assemblies. Please shed some light on why this exception is occurring. Thank you for your time.

NinjaLlama
  • 167
  • 3
  • 14
  • Defining a setting as a stringcollection doesnt initialize the collection. You have to test it the first time (or add a dummy item) as noted in the duplicate: [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Jul 05 '20 at 02:27
  • So why would it work on an unsigned assembly then? I can just go in and check/uncheck the box to fail/succeed respectively. If it was not initialized shouldn't it fail every time? – NinjaLlama Jul 05 '20 at 02:34
  • 1
    You're just discovering what will happen when you run your program on another machine. That setting will be null there as well, and die the same way. What's specific about signing it is that the identity of the user.config file changed, so the old one no longer gets used. You must check for this in your code, null check required. – Hans Passant Jul 05 '20 at 21:48
  • Thank you @HansPassant I guess I initialized the collection at some point. I will have to be more diligent in my checks. – NinjaLlama Jul 05 '20 at 23:24

0 Answers0