0

this is my first post here and I'm also new to C# / OO and LinqPAD. (translate to: I may mis-use concepts or terms due to being in 'learning mode')

I have an application that I've narrowed my bug down to the following query. I decided to try out LinqPAD to solve the issue in a constrained environment.

We are using Entity Framework for our database interactions. I am using LinqPAD's "C# Program" selection.

I found a great tutorial on how to use entities in LinqPAD and this seems to be working. (Proof: 'aEntities' does not return an error like it did when the entities were not connected properly)

So let's get to the issue.

At the 'var' I get: "NullReferenceException: Object reference not set to an instance of an object."

The reason I'm here asking this is every single example I see out there uses this 'var' approach. It does not work for some reason. I've tried declaring 'qryDC', creating a new instance of it and then assigning it. (I'm feeling weak knowledge wise here, but am reading and learning.)

I have read John Saunders epic write up about this topic. (Wow man! Thanks!) What is a NullReferenceException, and how do I fix it?

I however was not able to translate that knowledge to address my issue (learning curve).

Here is the code:

void Main(AEntities aEntities)
{

  decimal fmProjectUID = 1123;

  var qryDC = 
        from pnp in aEntities.PNonPRs 
            from p in aEntities.Projects
            from pt in aEntities.PurchaseTypes
            from wbs in aEntities.P32
            from pc in aEntities.PPhases
            where pnp.ProjectUID == p.ProjectUID
            where p.ProjectUID == fmProjectUID
            where pnp.PurchaseTypeUID == pt.PurchaseTypeUID
            where pnp.P32UID == wbs.P32UID
            where pt.IsNonPR == 1
            orderby pt.PurchaseTypeID
            select new 
            {
                PNonPrUID = pnp.PNonPrUID
                ,PurchaseTypeID = pt.PurchaseTypeID
                ,PhaseCode = pnp.ProjectPhase.PhaseCode
                ,DANbr = pnp.AType.DANbr
                ,PreAmt = pnp.PreAmt
                ,POStDate = pnp.POStDate
                ,POFDate = pnp.POFDate
                ,BaseAmt = pnp.BaseAmt
                ,Notes = pnp.Notes
        ,ChangedByName = pnp.ChangedByName
            };

 }

Let me know if you need more info.

And thank you for your time, effort and thoughts.

Community
  • 1
  • 1

1 Answers1

4

You can't just arbitrarily assign arguments to the Main method and expect them to be filled in. Your aEntities variable is null when you run this, thus the null reference exception. You need to instantiate your context.

void Main()
{
    decimal fmProjectUID = 1123;

    var aEntities = new AEntities();
    var qryDC = from pnp in aEntities.PNonPRs 
                from p in aEntities.Projects
                from pt in aEntities.PurchaseTypes
                // etc...
}

Alternatively, if you defined your connection in LINQPad and then selected said connection for the query you can omit the creation of the AEntities and just use the following.

void Main()
{
    decimal fmProjectUID = 1123;

    var qryDC = from pnp in PNonPRs 
                from p in Projects
                from pt in PurchaseTypes
                // etc...
}

I would also suggest you familiarize yourself with the debugger in Visual Studio and LINQPad. Had you set a breakpoint and examined the variables you would have seen that aEntities was null.

Craig W.
  • 16,585
  • 6
  • 44
  • 77
  • Thank you so much for following up. I did point LinqPAD at the DLL for the application so it was able to use the connection string. Removing the "aEntities" took care of the issue. Thank you for pointing out where the null was coming from.To follow on, I have been learning the debugger, once you get to know it it's pretty amazing however was not working for this issue. I do need to get work to pay for a licensed copy of LinqPAD though so I can debug there. Thank you again. – Brent Nicholas May 20 '16 at 17:43