1

Ok I'm having this problem that is driving me crazy, I'm doing a search for a website, and it works great on my machine but when I upload it to gearhost it just blow with this error :

Object reference not set to an instance of an object.

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.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 43: @if (Model != null)
Line 44: {
Line 45: if (Model.Any())
Line 46: {

The error appear on Line 45, even though I'm sure I know its not null and its and IEnumerable.

My Model in razor is declared as follow:

@model IEnumerable<Posada> 

And this does work if I send an empty search, but not when I use a string that returns rows or other that does not return any.

In my controller I just return a IEnumerable and use it as the model.

 IEnumerable<Posada> posadas = unitOfWork.PosadaRepository.Get(includeProperties: "Estado,Lugar")
                .Where(p => p.Nombre.ToLowerInvariant().Contains(query) ||
                            p.Estado.Nombre.ToLowerInvariant().Contains(query) ||
                            p.Lugar.Nombre.ToLowerInvariant().Contains(query))

                .OrderBy(p => p.Id)
                .Skip((page - 1)*pagingInfo.ItemsPerPage)
                .Take(pagingInfo.ItemsPerPage);
2
  • 3
    Model won't be null if you just set it to an IEnumerable, however, once you call Any(), all of your Where, OrderBy, and other LINQ methods will actually trigger. My thought is something in p you are dotting off of is null. Commented Jun 1, 2015 at 23:30
  • 2
    TyCobb you are right.. I knew it was a dumb mistake but really was not able to see it, thanks. Commented Jun 1, 2015 at 23:33

2 Answers 2

3

Seems like NullReferenceException happens when you enumerate IEnumerable (when you execute Any()). IEnumerable is not null, and then most likely one gets NullReferenceException:

p.Nombre.ToLowerInvariant()
p.Estado.Nombre.ToLowerInvariant()
p.Lugar.Nombre.ToLowerInvariant()
pagingInfo.ItemsPerPage
Sign up to request clarification or add additional context in comments.

Comments

1

Remember IEnumerable<> only evaluates the query when it needs to, so as stated by the other answer, when you use Any(), your query gets evaluated and in that case either Estado or Lugar can be null.

Try something like:

(p.Estado != null && p.Estado.Nombre.ToLowerInvariant().Contains(query)) ||
(p.Lugar != null && p.Lugar.Nombre.ToLowerInvariant().Contains(query)))

1 Comment

is that called numbre uno which is presented at advertising?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.