-1

During the construction of my Window LoginSystem, a NullReferenceException is being thrown only when running the application through the .exe.

While debugging, everything works perfectly fine however.

Code which calls the LoginSystem window:

LoginSystem ls = new LoginSystem();
ls.Show();

Where I've found the problem to be in my LoginSystem class:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    Login.con = new SqlConnection(ConfigurationManager.ConnectionStrings["thuisDB"].ConnectionString);
    ...
}

Just in case you're wondering:

public class Login
{
    public static SqlConnection con = null;
    ...
}

Link to stack trace: HERE

PS: This line (Login.con = new SqlConnection(....) is the first time Login.con gets called, as the only code using that static var are in a class which LoginSystem is supposed to make.

EDIT: This question is NOT about me asking what a NullRef is or how to fix it, it was merely a single event where I didn't know why it was being thrown & didn't know how to debug it.

Yorrick
  • 377
  • 2
  • 8
  • 28
  • Add more code, specially where the `NullPointerException` happens. – EduardoFernandes May 22 '15 at 03:04
  • That's the only code that should be relevant though, don't think any of my other code would be relevant at all to this NullReference – Yorrick May 22 '15 at 03:05
  • It sounds like `ConfigurationManager.ConnectionStrings` is null, or the result coming out of the collection. Try doing `var string = ConfigurationManager.ConnectionStrings["thuisDB"];` and see if the value is null, before accessing `ConnectionString` – Johnathon Sullinger May 22 '15 at 03:09
  • That thought has crossed my mind as well, however I'm not entirely sure as to how I should test for that :| – Yorrick May 22 '15 at 03:11
  • 2
    Just access each component one at a time. `var conStrings = Configurationmanager.ConnectionStrings;` then `var result = conStrings["thuisDB"];` then lastly `string str = result.ConnectionString;`. After each step, do a null check before moving forward. – Johnathon Sullinger May 22 '15 at 03:12
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Alex May 22 '15 at 03:16
  • @Alex Hardly, I'm not asking what a NullRef is, I was asking why it was being thrown, as I thought the app.config was embedded in the .exe, now that I know it's not, it's an easy fix. – Yorrick May 22 '15 at 10:13

1 Answers1

1

ConnectionStrings["thuisDB"] will be null if you are running the .exe in a folder where it can't find the configuration file.

Look for something like MyProgram.exe.config and make sure that is in the same folder as your executable file.

Kazetsukai
  • 2,268
  • 1
  • 14
  • 16
  • 1
    It's not just a matter checking the config is in the same folder as the exe, you also have to make sure you've got permission to access it - you can't just indiscriminately hit your app location under %PROGRAMFILES% anymore. – slugster May 22 '15 at 03:38
  • @Kazetsukai I see, I thought the app.config was automatically added to the exe as some sort of resource. – Yorrick May 22 '15 at 10:13