[.Net] Editor + SearchPanes leads to "Object reference not set to an instance of an object."

[.Net] Editor + SearchPanes leads to "Object reference not set to an instance of an object."

Vincent.PVincent.P Posts: 35Questions: 6Answers: 0
edited May 2021 in SearchPanes

Hello everyone,

I recently switched the backend of a project from PHP to .Net for internal reasons, and it seems like that there is an issue with the .Net library when using the SearchPanes feature and the Editor with server side processing enabled.

Here is a short reduced case:

var editor = new Editor(db, "scraping_consult")
                    .Field(new Field("scraping_consult.id")
                        .Set(Field.SetType.Create)
                    )
                    .Field(new Field("scraping_consult.creation_date")
                        .Set(Field.SetType.Create)
                    )
                    .Field(new Field("scraping_consult.sector")
                        .SetFormatter(Format.IfEmpty(null))
                        .SearchPaneOptions(new SearchPaneOptions()
                            .Table("scraping_consult")
                            .Value("sector")
                        )
                    );

var result = editor.TryCatch(false)
                  .Debug(true)
                  .Process(Request.Form)
                  .Data();

For a reason that I can't figure, adding .SearchPaneOptions(new SearchPaneOptions() …) generates the following Exception:

System.NullReferenceException: Object reference not set to an instance of an object.
   at DataTables.SearchPaneOptions.Exec(Field fieldIn, Editor editor, List`1 leftJoinIn, DtRequest http, Field[] fields)
   at DataTables.Field.SearchPaneOptionsExec(Field field, Editor editor, List`1 leftJoin, Field[] fields, DtRequest http)
   at DataTables.Editor._Get(Object id, DtRequest http)
   at DataTables.Editor._Process(DtRequest data)
   at DataTables.Editor.Process(DtRequest data)
   at DataTables.Editor.Process(IEnumerable`1 data, String culture)
   at asptest_noauth.Pages.ConsultModel.OnPost() in C:\Users\Vincent\asptest-noauth\Pages\Consult.cshtml.cs:line 207
   at lambda_method26(Closure , Object , Object[] )
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory.ActionResultHandlerMethod.Execute(Object receiver, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeHandlerMethodAsync()
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeNextPageFilterAsync()
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeInnerFilterAsync()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

I don't get any debug information as the server returns an error 500.

Any idea what am I doing wrong here?

Vincent.

Answers

  • sandysandy Posts: 913Questions: 0Answers: 236

    Hi @Vincent.P ,

    This isn't something that I have come across before. In the example that I have locally there is a field as follows.

    .Field(new Field("users.phone")
        .SearchPaneOptions(new SearchPaneOptions()
            .Table("users")
            .Value("phone")
        )
    )
    

    So I'm not sure exactly why yours is not working. Even when I add the formatter that you have it works fine. Can you try and Identify which line in SearchPaneOptions.Exec is throwing the error?

    Thanks,
    Sandy

  • Vincent.PVincent.P Posts: 35Questions: 6Answers: 0

    Hi @sandy,

    Here is the error with the line numbers:

    System.NullReferenceException: Object reference not set to an instance of an object.
             at DataTables.SearchPaneOptions.Exec(Field fieldIn, Editor editor, List`1 leftJoinIn, DtRequest http, Field[] fields) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/SearchPaneOptions.cs:line 258
             at DataTables.Field.SearchPaneOptionsExec(Field field, Editor editor, List`1 leftJoin, Field[] fields, DtRequest http) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Field.cs:line 778
             at DataTables.Editor._Get(Object id, DtRequest http) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Editor.cs:line 1461
             at DataTables.Editor._Process(DtRequest data) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Editor.cs:line 1216
             at DataTables.Editor.Process(DtRequest data) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Editor.cs:line 887
             at DataTables.Editor.Process(IEnumerable`1 data, String culture) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Editor.cs:line 920
    

    Maybe I should also say that this is on an ASP.Net Razor pages project, so it might be the difference that makes it happen?

    Best regards,
    Vincent

  • sandysandy Posts: 913Questions: 0Answers: 236

    Hi @Vincent.P ,

    There are a couple of things that it could be on that line. Would you be able to link to a test case please so that I can see the request and response that are going to and from the server? Failing that could you paste them in here please?

    Thanks,
    Sandy

  • Vincent.PVincent.P Posts: 35Questions: 6Answers: 0
    edited May 2021

    Hi @sandy,

    The project isn't deployed on a server yet, but on my local machine, here are the request and response:

    Request:

    draw    "1"
    columns[0][data]    "scraping_consult.id"
    columns[0][name]    ""
    columns[0][searchable]  "true"
    columns[0][orderable]   "true"
    columns[0][search][value]   ""
    columns[0][search][regex]   "false"
    columns[1][data]    "scraping_consult.creation_date"
    columns[1][name]    ""
    columns[1][searchable]  "true"
    columns[1][orderable]   "true"
    columns[1][search][value]   ""
    columns[1][search][regex]   "false"
    columns[2][data]    "srcname"
    columns[2][name]    ""
    columns[2][searchable]  "true"
    columns[2][orderable]   "true"
    columns[2][search][value]   ""
    columns[2][search][regex]   "false"
    columns[3][data]    "scraping_consult.title_nl"
    columns[3][name]    ""
    columns[3][searchable]  "true"
    columns[3][orderable]   "true"
    columns[3][search][value]   ""
    columns[3][search][regex]   "false"
    columns[4][data]    "scraping_consult.title_fr"
    columns[4][name]    ""
    columns[4][searchable]  "true"
    columns[4][orderable]   "true"
    columns[4][search][value]   ""
    columns[4][search][regex]   "false"
    columns[5][data]    "scraping_consult.links_nl"
    columns[5][name]    ""
    columns[5][searchable]  "true"
    columns[5][orderable]   "true"
    columns[5][search][value]   ""
    columns[5][search][regex]   "false"
    columns[6][data]    "scraping_consult.links_fr"
    columns[6][name]    ""
    columns[6][searchable]  "true"
    columns[6][orderable]   "true"
    columns[6][search][value]   ""
    columns[6][search][regex]   "false"
    columns[7][data]    "scraping_consult.relevance"
    columns[7][name]    ""
    columns[7][searchable]  "true"
    columns[7][orderable]   "true"
    columns[7][search][value]   ""
    columns[7][search][regex]   "false"
    columns[8][data]    "scraping_consult.relevance_TZBW"
    columns[8][name]    ""
    columns[8][searchable]  "true"
    columns[8][orderable]   "true"
    columns[8][search][value]   ""
    columns[8][search][regex]   "false"
    columns[9][data]    "scraping_consult.signal_created"
    columns[9][name]    ""
    columns[9][searchable]  "true"
    columns[9][orderable]   "true"
    columns[9][search][value]   ""
    columns[9][search][regex]   "false"
    columns[10][data]   "users"
    columns[10][name]   ""
    columns[10][searchable] "false"
    columns[10][orderable]  "false"
    columns[10][search][value]  ""
    columns[10][search][regex]  "false"
    columns[11][data]   "scraping_consult.comment"
    columns[11][name]   ""
    columns[11][searchable] "true"
    columns[11][orderable]  "true"
    columns[11][search][value]  ""
    columns[11][search][regex]  "false"
    columns[12][data]   "domains"
    columns[12][name]   ""
    columns[12][searchable] "false"
    columns[12][orderable]  "false"
    columns[12][search][value]  ""
    columns[12][search][regex]  "false"
    columns[13][data]   "sectors"
    columns[13][name]   ""
    columns[13][searchable] "false"
    columns[13][orderable]  "false"
    columns[13][search][value]  ""
    columns[13][search][regex]  "false"
    order[0][column]    "1"
    order[0][dir]   "desc"
    order[1][column]    "2"
    order[1][dir]   "asc"
    start   "0"
    length  "50"
    search[value]   ""
    search[regex]   "false"
    

    Request headers:

    Accept
        application/json, text/javascript, */*; q=0.01
    Accept-Encoding
        gzip, deflate, br
    Accept-Language
        fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3
    Cache-Control
        no-cache
    Connection
        keep-alive
    Content-Length
        3375
    Content-Type
        application/x-www-form-urlencoded; charset=UTF-8
    Cookie
        .AspNetCore.Antiforgery.<secret_token>
        1
    Host
        localhost:5001
    Origin
        https://localhost:5001
    Pragma
        no-cache
    Referer
        https://localhost:5001/Overview/Consult
    RequestVerificationToken
        secret_token
        Trailers
    User-Agent
        Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0
    X-Requested-With
        XMLHttpRequest
    

    Response:

    System.NullReferenceException: Object reference not set to an instance of an object.
       at DataTables.SearchPaneOptions.Exec(Field fieldIn, Editor editor, List`1 leftJoinIn, DtRequest http, Field[] fields) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/SearchPaneOptions.cs:line 258
       at DataTables.Field.SearchPaneOptionsExec(Field field, Editor editor, List`1 leftJoin, Field[] fields, DtRequest http) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Field.cs:line 778
       at DataTables.Editor._Get(Object id, DtRequest http) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Editor.cs:line 1461
       at DataTables.Editor._Process(DtRequest data) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Editor.cs:line 1216
       at DataTables.Editor.Process(DtRequest data) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Editor.cs:line 887
       at DataTables.Editor.Process(IEnumerable`1 data, String culture) in /home/vagrant/DataTablesSrc/extensions/Editor-NET/DataTables-Editor-Server/Editor.cs:line 920
       at project.Pages.ConsultModel.OnPost() in C:\Users\Vincent\project\Pages\Overview\Consult.cshtml.cs:line 209
       at lambda_method26(Closure , Object , Object[] )
       at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory.ActionResultHandlerMethod.Execute(Object receiver, Object[] arguments)
       at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeHandlerMethodAsync()
       at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeNextPageFilterAsync()
       at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
       at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
       at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeInnerFilterAsync()
       at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
       at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
       at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
       at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
    --- End of stack trace from previous location ---
       at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
       at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
       at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
       at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
    
    HEADERS
    =======
    Cache-Control: no-cache
    Pragma: no-cache
    Content-Type: application/x-www-form-urlencoded; charset=UTF-8
    :authority: localhost:5001
    :method: POST
    :path: /Overview/Consult
    :scheme: https
    Accept: application/json, text/javascript, */*; q=0.01
    Accept-Encoding: gzip, deflate, br
    Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3
    Cookie: PHPSESSID=eb8tjkkd17d05381e36jsl1n26,.AspNetCore.Antiforgery<secret_response_token>
    Host: localhost:5001
    Referer: https://localhost:5001/Overview/Consult
    TE: trailers
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0
    DNT: 1
    Origin: https://localhost:5001
    Content-Length: 3375
    requestverificationtoken: secret_request_token
    x-requested-with: XMLHttpRequest
    
  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    Can you let us know what version of the Editor .NET libraries you are using please? If not the current release of 2.0.2, it would be worth upgrading.

    If that doesn't solve it, could you show us the full controller you are using with the Editor .NET initialisation so we can attempt to reproduce the error here. Unfortunately, we aren't seeing anything obvious in the code.

    Thanks,
    Allan

  • Vincent.PVincent.P Posts: 35Questions: 6Answers: 0

    Hi @allan , I am using the latest version of the Editor (2.0.2).

    I think I am also using the latest version of the js libraries (if it is also relevant).

    Here you can find the controller that is used: https://gist.github.com/Ezneh/d6a9e02dc6676c00ca2c06a111f7745a

    Keep in mind it is an ASP.Net MVC Razor application.

  • Vincent.PVincent.P Posts: 35Questions: 6Answers: 0

    As an update: I also provided the javascript that is used to retrieve the fields as well

  • fostercarlyfostercarly Posts: 1Questions: 0Answers: 0

    If you try to access a member of a class (here MyClass) then you get a System.NullReferenceException. Which is the same as "object reference not set to an instance of an object" . It indicates that you are trying to access member fields, or function types, on an object reference that points to null. That means the reference to an Object which is not initialized. To prevent the error, objects that could be null should be tested for null before being used.

    if (mClass != null)
    {
    // Go ahead and use mClass
    mClass.property = ...
    }
    else
    {
    // Whoops! mClass is null and cannot be used without first assigning it to an instance reference
    // Attempting to use mClass here will result in NullReferenceException
    }

Sign In or Register to comment.