views:

59

answers:

4

I have the following sentence:

var customers = from customer in Context.ps_customer
                select customer;

As you can see, it is the most simple sentence in the world. Well, it throws a NullReferenceException, and I don't have any idea why. in fact, the exception is thrown at

List<ps_customer> clientes = customers.ToList<ps_customer>();

but if I set a breakpoint in the Linq sentence and try to see the customers value, I have the NullReferenceException.

Does anyone have any idea why I get this exception?

EDIT: I am going to provide a bit more information:

MyEntityModel Context = new MyEntityModel();

var solicitudes = from  solicitud in Context.ps_orders
                  where solicitud.date_add.Year == fecha.Year &&
                        solicitud.date_add.Month == fecha.Month &&
                        solicitud.date_add.Day == fecha.Day
                  select solicitud;

//This return correct data
ps_orders orden = solicitudes.ToList<ps_orders>().FirstOrDefault(); 

var customers = from customer in Context.ps_customer
                where customer.id_customer == orden.id_customer
                select customer;

var orden_detalles = from oDetalle in Context.ps_order_detail
                     where oDetalle.id_order == orden.id_order
                     select oDetalle;

var direcciones = from oDireccion in Context.ps_address
                  where oDireccion.id_address == orden.id_address_delivery
                  select oDireccion;

ps_address direccion = direcciones.FirstOrDefault(); //Correct data
List<ps_order_detail> detalles = orden_detalles.ToList<ps_order_detail>(); //Correct data
ps_customer clientes = customers.FirstOrDefault(); //NullReferenceException

I am totally sure that ps_customer has data, 2 rows to be specific, and I have deleted the ps_customer entity from the .edmx and I have added it again, an it still happens

Thank you very much!

EDIT 2: I have copied the create statement of the table, created a new table called customerTwo, inserted new data, and it still fails... By the way, I am using MySQL, and the DataBase is created by Prestashop, just in case that information is useful...

A: 

You're getting this exception because Context.ps_customer is null when your LINQ query is evaluated. Based on your second line of code it looks like your query should be the following:

var customers = from customer in clientes
                select customer;

But, since you haven't provided many details I cannot say that is the problem for sure.

Brian Driscoll
It cannot be that sentence because "clientes" is defined after the query, it is a variable to store the query's result. By the way, I am going to edit the question to include more information. Thanks to answer!
DarthRoman
A: 

EDIT : Try this query -

var customers = (from c in Context.ps_customer
                select c).ToList();

If c returns Count=0 it means your table didnt have any entries. So that is why the null issues...anyways I would recommend debugging carefully..and determining where exactly it throws the exception sometimes you are just looking at the wrong thing when the problem maybe with something else.

Original : customers has null..obviously means for some reason your table ps_customer returns null are you sure it has data and model is right...also it might be worth looking at your code..my guess is check if your Context is null

Misnomer
I am sure that the table has data, two rows to be specific. And Context is not null, I have checked that. In fact, I have two other queries and both of them return something, so for some reason the only wrong table is ps_customer...
DarthRoman
I have updated my answer on what I think maybe the problem.
Misnomer
A: 

I'm going to post another answer after looking at your edit.

The problem as I see it is that you're assuming that the value returned from this:

ps_orders orden = solicitudes.ToList<ps_orders>().FirstOrDefault(); 

is going to have a value for id_customer that matches one or more rows from Context.ps_customer, which is fallible because it's susceptible to delete anomolies (ie: if the order returned by FirstOrDefault() refers to a customer that has been deleted from the database, then your customer query will return no results).

I'd recommend using another query approach, perhaps joining on the two tables rather than trying to match against a specific value for id_customer, say something like this:

//disclaimer: I'm primarily a SQL dev, not LINQ or EF, so this syntax may be wrong
var customers = from customer in Context.ps_customer
join orden in solicitudes
on customer.id_customer == orden.id_customer
select customer;
Brian Driscoll
I have checked that too, and for sure there is one row with the correct id_customer. Besides, in order to simplify the query, I tried to make the most simple query, this is, without "where", as you can see in my first post, and it still crashes.
DarthRoman
Well, you wouldn't be getting this error if everything was non-null... have you tried setting breakpoints and running this in debug mode to isolate the data member that's null, and then backtrack from there? For the record I still think that Context.ps_customer is null...
Brian Driscoll
Yes, I've tried to run it in debug mode, but the problem is that Linq uses deffered operations, so until the very moment I do the customers.FirstOrDefault(), it doesn't execute the code, and I cannot know the concrete problem because it only fails in that moment, and the error is just NullReferenceException...
DarthRoman
Are you looking through the stacktrace and/or checking inner exceptions?
Brian Driscoll
A: 

It looks to me that ps_orders orden = solicitudes.ToList<ps_orders>().FirstOrDefault(); is returning null. You aren't seeing it until you attempt to evaluate customers.FirstOrDefault() due to deferred evaluation. I'd suggest verifying that solicitudes is correctly defined.

Additionally, you may want to consider reducing the amount of database traffic by consolidating queries where you can - FirstOrDefault() can be applied to an entity query directly, which usually translates to a SELECT TOP 1 query. Contrast this with ToList().FirstOrDefault(), which says to the database "give me everything", and then selects the first record. Just a tip!

Ben