Skip to content

Unhandled NullReferenceException being thrown #57

Closed

Description

@musabhusaini
Issue:

A NullReferenceException is being thrown when trying to call Next that has no result.

Scenario:

Assuming document is a CQ object, I am making a call similar to:

var elem = document["#content.results > table > tbody > tr > td:nth-child(1) > a:nth-child(1)"].First();
// Do something with elem
var link = elem.Parent().Next("td").Children("a").First();
// NullReferenceException thrown.

In fact, there is no Next("td"), but as I understand it, the call should fail silently and return an empty CQ object. Instead, a NullReferenceException is thrown. Let me know if I can provide more useful diagnostic information.

Exception Details:
System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=CsQuery
  StackTrace:
       at CsQuery.Engine.SelectorEngine.Select(IEnumerable`1 context)
       at CsQuery.Engine.Selector.Select(IDomDocument document, IEnumerable`1 context)
       at CsQuery.Engine.Selector.<Filter>d__6.MoveNext()
       at CsQuery.CQ.AddSelection(IEnumerable`1 elements)
       at CsQuery.CQ.ConfigureNewInstance(CQ dom, IEnumerable`1 elements, CQ context)
       at CsQuery.CQ.NewInstance(IEnumerable`1 elements, CQ context)
       at CsQuery.CQ.FilterIfSelector(String selector, IEnumerable`1 list, SelectionSetOrder order)
       at CsQuery.CQ.nextPrevImpl(String selector, Boolean next)
       at CsQuery.CQ.Next(String selector)
       ...

Activity

  1. jamietre commented on Oct 31, 2012

    @jamietre
    Owner

    I just tried this with some random HTML that matches nothing (so there are no results at any step) and it works fine. Can you confirm you're using 1.3, and if so show me some html that fails with this selector?

  2. musabhusaini commented on Oct 31, 2012

    @musabhusaini
    Author

    Yes, I am using 1.3. I loaded up this page using CQ.CreateDocument(string html) and the following code failed for me:

    document.Cq()["#content.results > table > tbody > tr > td:nth-child(1) > a:nth-child(1)"].Eq(19).Parent().Next("td").Children("a").First()
  3. jamietre commented on Oct 31, 2012

    @jamietre
    Owner

    OK tracked it down, fix shortly.

  4. musabhusaini commented on Oct 31, 2012

    @musabhusaini
    Author

    Great!

  5. added a commit that references this issue on Oct 31, 2012
  6. jamietre commented on Oct 31, 2012

    @jamietre
    Owner

    Fix on github now. There will be an update to nuget whenever I finish dealing with the encoding issue (#52) but the dist code here should solve this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions