views:

53

answers:

1
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: mapping

Source Error: 


Line 45:     #endregion
Line 46:        
Line 47:        public db() : 
Line 48:                base(global::data.Properties.Settings.Default.nanocrmConnectionString, mappingSource)
Line 49:        {

this is what i get if i implement such class:

partial class db
{
    static db _db = new db();

    public static db GetInstance()
    {
        return _db;
    }
}

db is a linq2sql datacontext

why this hapenned and how to solve this?

UPD: that file is generated by linq2sql:

    private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();

    public db() : 
            base(global::data.Properties.Settings.Default.nanocrmConnectionString, mappingSource)
    {
        OnCreated();
    }

if I instantiate db inside method (not property like here) all works fine. and the static method worked until this morning, but now even 2 days ago version (restored from repository) falls with the same error.

UPD 2:

so this is my partial class after problem was solved:

namespace data
{
using System.Data.Linq.Mapping;

partial class db
{
    static db _db = new db(global::data.Properties.Settings.Default.nanocrmConnectionString, new AttributeMappingSource());

    public static db GetInstance()
    {
        return _db;
    }
}
}
A: 

Ah, I just had a look. It seems the static variable is not initialized for some reason.

For now you can solve the problem by doing the following:

static db _db = new db(
 global::data.Properties.Settings.Default.nanocrmConnectionString, 
 new AttributeMappingSource());

It is rather strange though that mappingSource would still be null.

Thinking about it now, it could be how partial classes are stitched together. For some reason, it uses your code as the 'prefix' of the entire class. It seems, as I would expect, that mappingSource is not initialized when _db is being initialized.

A further explanation of what is causing the issue.

The order of initialization of static members are undefined, but generally they tend to be in order.

Take the follow program as an example to further complicate things.

Main.cs

  class Printer
  {
    public Printer(string s)
    {
      Console.WriteLine(s);
    }
  }

  partial class Program
  {
    static void Main()
    {
      new Program();
      Console.ReadLine();
    }
  }

X.cs

  partial class Program
  {
    static Printer x = new Printer("x");
  }

Y.cs

  partial class Program
  {
    static Printer y = new Printer("y");
  }

Z.cs

  partial class Program
  {
    static Printer z = new Printer("z");
  }

Now depending on how feed the compiler the classes, the order of initialization can change.

Try:

  • csc Main.cs X.cs Y.cs Z.cs
  • csc Main.cs Y.cs Z.cs X.cs
  • csc Main.cs Y.cs X.cs Z.cs

I suspect you will see different results every time.

leppie
yep, i discovered this hack too, but due to i'm just learning .net programming it would be nice if someone will point me to **my** error. it's definitely mine, i'm pretty sure :-)
zerkms
@zerkms: Updated answer, it could be a bug or by design. It is not *your* error. I would recommend against using a static `DataContext`.
leppie
this would be true if that code hadn't worked until today morning... the saddest thing is that i do nothing related to linq :-(
zerkms
@itowlson: hehe, nice catch. this sounds realistic :-)
zerkms
why @itowlson's post has been deleted???
zerkms
@zerkms: Updated answer again, with big example :)
leppie
@leppie: yep, i got that from deleted itowlson's comment. so - this is an exhaustive exlpanation, thank you :-)
zerkms