Reflect: Method on nil interface value

I have the following code in single.html

{{ if .Resources.GetMatch "image" }}
{{ $preview := .Resources.GetMatch "intro" }}
{{ with $preview }}
{{ $scaled := .Resize "1045x" }}
<img class="image main" src="{{ $scaled.Permalink }}" alt="" />
{{ end }}
{{ else }}
{{ $preview := .Resources.GetMatch "intro" }}
<img class="image main" src="{{ $preview.Permalink }}" alt="" />
{{ end }

And I get this error when rendering the site:

reflect: Method on nil interface value

The if-clause is used because sometimes the resource is a SVG-file, which cannot be resized.

AFAICS all pages render fine, the files with JPGs or PNGs and the files with SVGs.

But maybe in one file the resources are not defined properly.

How can I debug this error? The error message does not refer to a specific page.

Update: While hugo server renders all files some files are missing in public when I just call hugo.

Meanwhile I think that my problem is that there is no way to determine whether an image is a raster image or a svg image. And as I have the following in my metadata I cannot find a svg by its extension.

---
resources:
- src: foobar.svg
  name: "intro"
---

Is there any way to determine if a resource is an svg or and jpg/png file?

1 Like

I found this workaround. At least it shows what I want to achieve.

In my content files I define an intro image:

---
resources:
- src: foobar.svg
  name: "introsvg"
---

if the image is a svg file.

And

---
resources:
- src: foobar.jpg
  name: "intro"
---

if the image is a resizable raster image.

Then in my template I make two if-statements:

{{ if .Resources.Match "intro" }}
{{ $preview := .Resources.GetMatch "intro" }}
{{ with $preview }}
{{ $scaled := .Resize "1045x" }}
<img class="image main" src="{{ $scaled.Permalink }}" alt="" />
{{ end }}
{{ end }}
{{ if .Resources.Match "introsvg" }}
{{ $preview := .Resources.GetMatch "introsvg" }}
<img class="image main" src="{{ $preview.Permalink }}" alt="" />
{{ end }}

I think there should be a better solution.

1 Like

I had “panic: reflect: Method on nil interface value [recovered]
panic: reflect: Method on nil interface value” because I used name.png in the shortcode instead of name.jpg. O_O