2

I have a static class called Helpers which contains a good number of simple helper methods including some simple extension methods on ‘string’ and the like:

public static string AddSquareBrackets(this string str)
{
     return "[" + str + "]";
}

I have a test class and method as follows:

[TestMethod()]
public void AddSquareBracketsTest()
{
     Assert.AreEqual("[NAME]", "NAME".AddSquareBrackets());
}

Static class declared with default (no coded) constructor:

namespace Equinoxe.Utilities.Helpers
{
    public static class HELPERS
    {

The call to the AddSquareBrackets throws the following:

System.TypeInitializationException was unhandled by user code
  Message=The type initializer for 'XXX.Utilities.Helpers.HELPERS' threw an exception.
  Source=XXX.Utilities
  TypeName=XXX.Utilities.Helpers.HELPERS
  StackTrace:
       at XXX.Utilities.Helpers.HELPERS.AddSquareBrackets(String str)
       at XXX.Utilities.Test.HELPERSTest.AddSquareBracketsTest() in C:\DEVELOPMENT\PROJECTS\XXX.NavEgate\XXX.Utilities.Test\HELPERSTest.cs:line 77
  InnerException: System.NullReferenceException
       Message=Object reference not set to an instance of an object.
       Source=XXX.Utilities
       StackTrace:
            at XXX.Utilities.Helpers.HELPERS..cctor() in C:\DEVELOPMENT\PROJECTS\XXX.Utilities\XXX.Utilities\Helpers\HELPERS.cs:line 44
       InnerException:{System.NullReferenceException: Object reference not set to an instance of an object.
   at XXX.Utilities.Helpers.HELPERS..cctor() in C:\DEVELOPMENT\PROJECTS\XXX.Utilities\XXX.Utilities\Helpers\HELPERS.cs:line 44}

I have also looked

Im running VS2010

Philip.ie
  • 1,506
  • 1
  • 17
  • 19

1 Answers1

3

Add a breakpoint at:

C:\DEVELOPMENT\PROJECTS\XXX.Utilities\XXX.Utilities\Helpers\HELPERS.cs:line 44

On that line, something is null, that should not be.

leppie
  • 115,091
  • 17
  • 196
  • 297
  • 2
    Thanks you pointed correctly. Line 44 populates a variable from the Web.Config file. Obviously the Web.Config file was not present in the test project. When added (by way of a link so I don’t have to update when original changes), all works. Many thanks for your help... – Philip.ie Apr 26 '12 at 09:49