0

I use app.config to store some info. My file looks like this:

<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="używajDomyślnie" value="false" />
<add key="sInicjały" value="ŁM" />
<add key="nPrzedstawiciel" value="Łukasz Motyczka" />
<add key="nPozycja" value="Przedstawiciel Naukowo-Handlowy" />
<add key="nTelefon" value="+48 784 567 670" />
<add key="nEmail" value="motyczka.lukasz@bmgrp.pl" />
<add key="dataoferty" value="" />
<add key="ostatniNrOferty" value="" />

 </appSettings>
 <connectionStrings>
 <add name="Oferty_BMGRP.Properties.Settings.BazaDanychConnectionString" connectionString="Data Source=|DataDirectory|\BazaDanych.sdf" providerName="Microsoft.SqlServerCe.Client.3.5" />
  </connectionStrings>
  <system.windows.forms jitDebugging = "true"/>
</configuration>

The snippet that throws a NullexceptionError is:

if(checkBox1.CheckState == CheckState.Checked)
    {

      Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
      AppSettingsSection app = config.AppSettings;

      //app.Settings.Clear();
      //app.Settings.Add("używajDomyślnie", "true");
      //app.Settings.Add("sInicjały", sInicjaly.ToString());
      //app.Settings.Add("nPrzedstawiciel", nPrzedstawiciel.ToString());
      //app.Settings.Add("nPozycja", nPozycja.ToString());
      //app.Settings.Add("nTelefon", nTelefon.ToString());
      //app.Settings.Add("nEmail", nEmail.ToString());

      config.AppSettings.Settings["używajDomyślnie"].Value = "true"; //<- THIS LINE GIVES ME AN ERROR
      config.AppSettings.Settings["sInicjały"].Value = sInicjaly.ToString();
      config.AppSettings.Settings["nPrzedstawiciel"].Value = nPrzedstawiciel.ToString();
      config.AppSettings.Settings["nPozycja"].Value = nPozycja.ToString();
      config.AppSettings.Settings["nTelefon"].Value = nTelefon.ToString();
      config.AppSettings.Settings["nEmail"].Value = nEmail.ToString();

      config.Save(ConfigurationSaveMode.Modified);

      form1.checkBox1.CheckState = CheckState.Checked;

    }    

When I debug it or build release in VS it works fine. Also it works on my laptop. But on every other machine I have null exception error. Also it worked until I tried to make a setup project (which works but meesed up this one). Whatmore the jit Debugging does not work, I still have a .net error (continue or exit).

Also I did notice that on my computer and some other laptop I was using, when error occurs it shows:

System.NullReferenceException: Odwołanie do obiektu nie zostało ustawione na wystąpienie obiektu.
   w Oferty_BMGRP.Form3.button1_Click(Object sender, EventArgs e) w C:\Users\user\Documents\Visual Studio 2010\Projects\BMGRP\Oferty BMGRP\OfertyBMGRP\Form3.cs:wiersz 282

Sorry for polish, but what I want to point is that on both machines it shows path to same folder, which exists only on one of them.

Please help :)

EDIT!

I just found out that if I build the release. Run application and make th above code run, it creates MyApplication.exe.config file, and then app works fine on every computer. How can I make it to work as setup project, where .exe.config is not created, only app.config?

Łukasz Motyczka
  • 1,119
  • 2
  • 10
  • 33
  • 1
    try `config.AppSettings.Add` instead maybe? The indexer (`Settings["blah"]`) is returning null here, before you even have the chance to set a value. – John Gibb Dec 12 '13 at 19:31
  • I hate when answer is so obvious... Yes, it doeas create .exe.config file this way. :0 Have to redo my code, because this way it will add these lines every time app is run to no end... Thank you – Łukasz Motyczka Dec 12 '13 at 19:41
  • @JohnGibb Please post it as an answer so i will be able to accept it :) – Łukasz Motyczka Dec 12 '13 at 20:26
  • 1
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Dec 12 '13 at 20:40
  • @ŁukaszMotyczka you got it – John Gibb Dec 12 '13 at 20:41

2 Answers2

2

MyApplication.exe.config is app.config, and you shouldn't mess with that paradigm. It's how .net applications are intended to work.

If you want to do your configuration differently than that, than you can either do your own file IO or find another external configuration library.

  • Well I have two files in my release folder. MyApplication.exe.config and app.config. I understand that they are basicaly the same file. But still it does not answer my question. Why did I get a nullreferenceexception before I run the program in VS and .exe.config was created? app.config file was already there. App on other computers do not create .exe.config during first run. – Łukasz Motyczka Dec 12 '13 at 19:29
  • 1
    @ŁukaszMotyczka did the framework add `app.config` to your output directory? or did you do that part yourself? – Sam I am says Reinstate Monica Dec 12 '13 at 19:31
  • 2
    @ŁukaszMotyczka You're program's not looking for `app.config` it's looking for `MyApplication.exe.config`. As far as your program is concerned, `app.config` doesn't even exist. – Sam I am says Reinstate Monica Dec 12 '13 at 19:32
  • it is there because it was set to copy always during building release. I know about that. Whanted to put it there because I thought that I will need that. – Łukasz Motyczka Dec 12 '13 at 19:40
  • 2
    @ŁukaszMotyczka you don't need to change the properties of `app.config` to make it work. you don't need to copy it during build. It's automatically copied in the form of `MyApplication.exe.config`, and that's the config file that your program uses – Sam I am says Reinstate Monica Dec 12 '13 at 19:46
2

You're trying to set the Value property of a null object. Settings["blah"] is returning null before you even have a chance to set the value.

Try this instead:

config.AppSettings.Add("key", "value");

Or maybe will a null check:

if(config.AppSettings.Settings["key"] == null)
{
   config.AppSettings.Add("key", "value");
}
John Gibb
  • 10,043
  • 2
  • 35
  • 46