Null pointer when using .Paginator.Pages

Hello!

I am trying to use the following to loop through some pages and create a display.

{{ range where .Paginator.Pages "Section" "research" }}
        {{ partial "research-card" .}}
      {{ end }}

When attempting to run this I receive the following error:
invalid memory address or nil pointer dereference

I have made a few pages but It doesn’t seem to see them i guess. It was working yesterday and now all of a sudden today the program does not run anymore.

Thanks!

Build the page collection, then paginate.

{{ range (.Paginate (where site.RegularPages "Section" "research")).Pages }}

This does the same thing, but might be easier to read:

{{ $pages := where site.RegularPages "Section" "research" }}
{{ range (.Paginate $pages).Pages }}
1 Like

Gives me the same error, just to make sure I understand correctly. “Section” means its looking at generated pages yes? I Wrote this code a while ago and I am revisiting now and so i don’t properly remember

I do not post untested code.

If your site structure is like this…

content/
├── post/
│   └── test.md
├── research/
│   ├── research-1.md
│   ├── research-2.md
│   └── research-3.md
└── _index.md

…the code will grab the pages from the content/research directory.

If you need additional assistance, please post a link to the public repository for your project.

I’m not doubting you. I’m simply saying that I’ve been troubleshooting this for 2 days and so far nothing has been working. Sadly where I work does not allow me to share publicly so I guess I will keep troubleshooting it till I find it.

Something feels odd because it was working yesterday and suddenly it breaks.
Thanks for the assist though I have noted to build the collection before using paginate

If you can’t share your repository for whatever reason, consider creating a dummy repo that you can share, which reproduces the problem you’re experiencing.

I will create a dummy repo soon but I have realised something and i don’t know if this will help make my issue more clear.

After thoroughly going through my code I have found that i use multiple ranges in the same html file but searching for different pages. The error gets thrown when I set pages belonging to services as “draft: false”. This makes me think that the 2 ranges are possibly clashing and causing the error.

Apologies for this info being spread like this but I am revisiting some old work. I will create a dummy repo and send it here soon.

      {{ range (.Paginate (where site.RegularPages "Section" "research")).Pages }}
        {{ partial "research-card" .}}
      {{ end }}

      {{ range (.Paginate (where site.RegularPages "Section" "services")).Pages }}
           {{ partial "services-card" .}}
      {{ end }}

You cannot paginate twice on the same page:

https://gohugo.io/templates/pagination/

If you call .Paginator or .Paginate multiple times on the same page, you should ensure all the calls are identical. Once either .Paginator or .Paginate is called while generating a page, its result is cached, and any subsequent similar call will reuse the cached result. This means that any such calls which do not match the first one will not behave as written.

But your “invalid memory address or nil pointer dereference” error is caused by something else.