views:

932

answers:

1

Hi. I think a lot of people have had this issue but I'm not able to fix it or understand why I'm having it. I've been tearing hair out for a couple of hours now.

I'm getting the error, "Object reference not set to an instance of an object." on my datacontext.SubmitChanges() on the SECOND time this method runs (I'm looping through a set of ObjectName strings):

    private Object CreateObject(string ObjectName, SystemClassEnum SystemClass)
    {
        Object result = new Object();
        result.Name = ObjectName;
        result.SystemClassID = (int)SystemClass;
        _dataContext.Objects.InsertOnSubmit(result);
        _dataContext.SubmitChanges();
        return result;
    }

I thought it was because the result.Name value can be null but I don't think that anymore.

It seems like somehow the dataContext is getting closed? but in Debug Mode I check the status of the connection and it's "Open" after the error occurs.

I'm using the repository pattern and ASP.NET MVC.

The result object (new Object()) is an instance of a LINQ DBML auto-generated class that also has a partial class that I created with a single extension method attached. I don't see how the extension to the class could be causing the problem.

I'm out of ideas.

Any thoughts? Thanks for any help you can provide!!

Best regards,

Eric

+2  A: 

Don't call one of your types Object - that is a seriously bad idea; you will regret it... pick a different name... (or System, etc).

Are you sure the error isn't actually on the line above (_dataContext.Objects. etc)? Unfortunately, you don't show any of the code relating to _dataContext (for example, could it have become null), and you don't indicate if (for example) you have added any partial methods to the data-context or entity, or have any events. I would expect the problem to be in one of those areas.

Marc Gravell
Ah, but Marc, using names like that is excellent code obfuscation. No need for any pesky post-compilation obfuscators that way. Characters with accents and dots that are not prominent in coding fonts can be used to make it nearly impossible to detect... :) namespace System { public class objẹct { } }
KristoferA - Huagati.com
@KristoferA- more like an excellent way to turn your code into a entry on daily WTF. @Marc- +1 for highlighting what a bad idea this is!
RichardOD
@RichardOD I was being sarcastic... :)
KristoferA - Huagati.com
This code occurs in my "repository" and in the constructor of it I have it create an instance of the DataContext from my DBML: private DataClassesDataContext _dataContext; public SQLServerMMRepository() { _dataContext = new DataClassesDataContext(); }The error thrown appears on the line: _dataContext.SubmitChanges();not: _dataContext.UserRelationshipInstances.InsertOnSubmit(result);I hear you on the capital Object thing I'm doing but I don't think that's the problem here.Why would my datacontext become "not a reference" after repeated calls?
vealer
I still don't have this resolved. Any thoughts? What I think my problem is is that I want to to transform, convert, cast, whatever the DBML Objects returned into DIFFERENT classes and have those classes also have extension methods that pull different views from the database. Creating an instance of my repository and of the business objects seems to make the DataContext choke. Any ideas on architecture for me?
vealer