Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2008

Tip: Looking for answers? Try searching our database.

NullReferencException in DLinq lambda method

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andrus - 14 May 2008 10:59 GMT
I tried DLinq code created by Marc and got NRE in ToList() line.
Any idea what causes this ?

Andrus.

Code:

Northwind db = CreateDB();
var orders = db.GetTable<Order>().ToArray().AsQueryable();

var query = from order in orders
                       select new
                       {
                           OrderID = order.OrderID,
                           Customer = new
                           {
                               ContactName = order.Customer.ContactName,
                               ContactTitle = order.Customer.ContactTitle
                           }
                       };
var list = query.ToList();

result:

System.NullReferenceException was unhandled
 Message="Object reference not set to an instance of an object."
 Source="Anonymously Hosted DynamicMethods Assembly"
 StackTrace:
      at lambda_method(ExecutionScope , Order )
      at System.Linq.Enumerable.<SelectIterator>d__d`2.MoveNext()
      at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
      at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

...
Jon Skeet [C# MVP] - 14 May 2008 11:42 GMT
> I tried DLinq code created by Marc and got NRE in ToList() line.
> Any idea what causes this ?

I suggest you find the query it's generated and look at the results.
Look for an order which doesn't have a customer, and check that the
query actually picks up the customer information to start with.

Jon
Marc Gravell - 14 May 2008 14:40 GMT
Jon's answer looks about right... as a quick check, try adding:

 where order.Customer != null

between the "from" and "select"; if it goes away, that is the problem.

Marc
Andrus - 14 May 2008 16:38 GMT
> Jon's answer looks about right... as a quick check, try adding:
>
>  where order.Customer != null
>
> between the "from" and "select"; if it goes away, that is the problem.

I added this check and NRE disappears.
However query does not return any rows in this case.
Is this expected result  from this query ?
Should it work without where clause also ?

Andrus.
Jon Skeet [C# MVP] - 14 May 2008 19:36 GMT
> > Jon's answer looks about right... as a quick check, try adding:
> >
[quoted text clipped - 6 lines]
> Is this expected result  from this query ?
> Should it work without where clause also ?

Have you looked at the generated SQL?

Signature

Jon Skeet - <skeet@pobox.com>
Web site: http://www.pobox.com/~skeet   
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

Frans Bouma [C# MVP] - 15 May 2008 09:48 GMT
> I tried DLinq code created by Marc and got NRE in ToList() line.
> Any idea what causes this ?
[quoted text clipped - 26 lines]
>       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
>       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

    ToList() enumerates the list, so it will crash if order.Customer is null.

        FB

Signature

------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------

Andrus - 15 May 2008 16:20 GMT
> ToList() enumerates the list, so it will crash if order.Customer is null.

new operator in query creates new object. so order.Customer *cannot*  be
null.

Andrus.
Marc Gravell - 16 May 2008 08:39 GMT
> new operator in query creates new object. so order.Customer *cannot*
 > be null.

No - the "new Customer" in the query posted only applies to the
anonymous type you are generating. It says nothing at all about whether
order.Customer is null or not.

Try simply enumerating the orders, and listing what Customer is...

Marc

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.