C#, cannot understand this error?

Posted by 5YrsLaterDBA on Stack Overflow See other posts from Stack Overflow or by 5YrsLaterDBA
Published on 2010-04-30T17:15:20Z Indexed on 2010/04/30 17:17 UTC
Read the original article Hit count: 160

Filed under:
|

I am using VS2008. I have a project, SystemSoftware project, connect with a database and we are using L2E. I have a RuntimeInfo class which contains some shared information there. It looks like this:

public class RuntimeInfo
    {
        public const int PWD_ExpireDays = 30;

        private static RuntimeInfo thisObj = new RuntimeInfo();

        public static string AndeDBConnStr = ConfigurationManager.ConnectionStrings["AndeDBEntities"].ConnectionString;

        private RuntimeInfo() {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns>Return this singleton object</returns>
        public static RuntimeInfo getRuntimeInfo()
        {
            return thisObj;
        }
}

Now I added a helper project, AndeDataViewer, to the solution which creates a simple UI to display data from the database for testing/verification purpose.

I don't want to create another set of Entity Data Model in the helper project. I just added all related files as a link in the new helper project.

In the AndeDataViewer project, I get the connection string from above RuntimeInfo class which is a class from my SystemSoftware project as a linked file. The code in AndeDataViewer is like this:

public class DbAccess : IDisposable
    {
        private String connStr = String.Empty;
        public DbAccess()
        {          
            connStr = RuntimeInfo.AndeDBConnStr;
        }
}

My SystemSoftware works fine that means the RuntimeInfo class has no problem there. But when I run my AndeDataViewer, the statement inside above constructor,

connStr = RuntimeInfo.AndeDBConnStr;

, throws an exception. The exception is copied here:

System.TypeInitializationException was unhandled
  Message="The type initializer for 'MyCompany.SystemSoftware.SystemInfo.RuntimeInfo' threw an exception."
  Source="AndeDataViewer"
  TypeName="MyCompany.SystemSoftware.SystemInfo.RuntimeInfo"
  StackTrace:
       at AndeDataViewer.DbAccess..ctor() in C:\workspace\SystemSoftware\Other\AndeDataViewer\AndeDataViewer\DbAccess.cs:line 17
       at AndeDataViewer.Window1.rbRawData_Checked(Object sender, RoutedEventArgs e) in C:\workspace\SystemSoftware\Other\AndeDataViewer\AndeDataViewer\Window1.xaml.cs:line 69
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
      ....
InnerException: System.NullReferenceException
       Message="Object reference not set to an instance of an object."
       Source="AndeDataViewer"
       StackTrace:
            at MyCompany.SystemSoftware.SystemInfo.RuntimeInfo..cctor() in C:\workspace\SystemSoftware\SystemSoftware\src\systeminfo\RuntimeInfo.cs:line 24
       InnerException: 

I cannot understand this because it looks fine to me but why there is an exception? we cannot access static variable when a class is a linked class? A linked class should be the same as the local class I think. "Linked" here means when I add file I use "Add As Link".

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET