Strange null reference problem within enum?

Hi All,

We've been having a strange problem. Recently we have seen null pointer exception coming out from our code. The exception trace is dead simple:


Message: The run has errored with description: Object reference not set to an instance of an object.
Object reference not set to an instance of an object.
-------------------------
User:
Source: mscorlib

Server stack trace:
at System.Collections.Hashtable.get_Item(Object key)
at WGP.Adapter.IntToPrataEnum(String prataCode, String val)
at WGP.Adapter.Curve.BuildCurves(Double[] date, Int32[] instrument, String[] currencies)
...

Anyway what really strange is that the code in IntToPrataEnum method is dead simple and straight forward. As far as I can see, I couldn't find a single problem here. Code is modified, but generally follows the same logic. 2 strings, switches and depending on the content return the correct enum cast into integer.

public static int IntToPrataEnum(string prataCode, string val)
{
  switch(prataCode)
  {
    case "Interpolation":
      switch(val)
      {
        case "LINEAR":
          return (int)WGP.InterpMethod.Linear;
        case "LOG_LINEAR":
          return (int)WGP.InterpMethod.LogLinear;
        case "SPLINE":
          return (int)WGP.InterpMethod.Cubic;
        default:
          throw new ArgumentException("Interpolation error");
      }
    case "Day Count":
      switch(val)
      {
        case "ACT/360":
        case "ACT_360":
          return (int)WGP.DayCount.Actual360;
        case "ACT_365":
        case "ACT/365":
          return (int)WGP.DayCount.Actual365Fixed;
        case "ACT_ACT":
        case "ACT/ACT":
          return (int)WGP.DayCount.ActualActual;
        default:
          throw new ArgumentException("Day count error");
      }
    default:
      throw new ArgumentException("Unrecognised parameter");
  }
}

Now the system is multi-threaded, and we're thinking there's something in .NET that makes initialisation and access of enum can throw a null pointer exception

The fact that it was thrown from a hashtable is odd, there's no reference of Hashtable in the method in question and I can only assume that's how enum is implemented internally within .NET

Any ideas

Cheers




Answer this question

Strange null reference problem within enum?

  • ZardoS42

    The WPG classes are unfortunately third party software and we don't have the debug build. I'm trying to source the original source code from the vendor. This only happen in 1 machine though and not in all other server, which very much pointing direction to race condition related to threading.

  • Suncho

    We can't tell for sure which lines it's breaking at as this is happening in production server, no pdb files are generated.

    InterpMethod and DayCount are defined as enums. Strange huh



  • WebService4Ever

    Well it would make sense if not all the WGP references are enums! This is why it's really strange.



  • Amos Soma

    Getting close to the real problem here. It's has nothing to do with enums. I decompile the code using Reflector and found out that it generates a Hashtable for one of the switch cases.

    So this is the possible breakage of the code because the stack trace shows that it is using the Hashtable. So obviously the CLR compiles the switch case into Hashtable. We just need to find out why.



  • Gianluca Colucci

    Hi,

    which line is it breaking on How are the InterpMethod and DayCount types defined, are they enums or a class

    Mark.



  • Binumohan

    Does the WPG class have a static onstructor where you are doing something with a hashtable Could be that the stack trace is not fully ocrrect if this is an optimized build of your app and is not showing the full picture, you should get a debug build on the machine with the pdb and see if you can reproduce it.

    Mark.



  • Trevor2006

    Error message is very clear, you are accessing a reference type which is not instanced yet. Posible problems can be that WGP is null or a source in property get of InterpMethod or DayCount properties that use some third reference variable. In such code an indexer is read from some collection which is null, 'get_Item'.


  • direwolfe

    Just received the source code, seems like all the enums are straight forward enums. They do have an attribute although it seems ti's only a marker attributes (implementation class is empty).



  • Strange null reference problem within enum?