0

I have a class library called SGDataLibrary with a class SGMemberDataOp.cs that holds all the data operations and gets the connection string to the database through the app.config.

Here is the connection string

 <add name="SimpleGym.Properties.Settings.SGYMConnectionString"
        connectionString="Data Source=owner\sqlexpress;Initial Catalog=SGYM;Integrated Security=True"
        providerName="System.Data.SqlClient" />

This is the SGMemberDataOp.cs class

namespace SGDataLibrary
{
   public class SGMemberDataOp
   {
      private string connectionString = Properties.Settings.Default.SGYMConnectionString;

Here is the code I am writing in my App.xaml.cs file

 public partial class App : Application
 {
     ---->private static SGMemberDataOp sgMemberDataOp = new SGMemberDataOp();
     public static SGMemberDataOp SGMemberDataOp
     {
        get { return sgMemberDataOp; }
     }
 }

The line with the arrow is throwing an exception

Object reference not set to an instance of an object

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Tim
  • 1,209
  • 4
  • 21
  • 33
  • 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 04 '12 at 19:10

1 Answers1

0

I just looked at some code where I do this but

private static SGMemberDataOp sgMemberDataOp;

 public static SGMemberDataOp SGMemberDataOp
 {
    get { return sgMemberDataOp; }
 }

public App()
{
    try
    {
        sgMemberDataOp = new SGMemberDataOp();
    }
    catch (Exception Ex)
    {
        MessageBox.Show(Ex.Message, "Startup failed", MessageBoxButton.OK);
    }
}

Newing it as a private did not work and not sure why.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • Thanks for the help marked yours as right however it was me being a fool and adding the wrong dll to the reference...Hate it when I do something like this Thanks again for your help – Tim Dec 04 '12 at 19:12