AppCrafter
AppCrafter

Reputation: 432

Spring / Thymeleaf: Property or field cannot be found on null, but still rendering

I have a Spring / Thymeleaf app that

org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Property or field 'projectName' cannot be found on null

However, the page looks normal. All variables are rendering with data. I'm just concerned that an exception is being thrown every request.

Here is the controller:

@Controller
@RequestMapping("/download")
public class AppDownloaderController {

    @Autowired
    InstallLinkJoinedService installLinkJoinedService;

    @RequestMapping(value = "/link/{installLink}", method = RequestMethod.GET)
    public String getInstallLink(Model model, @PathVariable("installLink") String installLink) {
        InstallLinkJoined installLinkJoined = installLinkJoinedService.getInstallLinkWithID(installLink);
        if (installLinkJoined != null) {
            model.addAttribute("install", installLinkJoined);
        }
        return "download";
    }
}

A snippet of the html in question:

<h3 class="achievement-heading text-primary" th:text="${install.projectName}"></h3>

The field is part of the InstallLinkJoined object:

@Column(nullable = false)
private String projectName;

And I have getters and setters for all fields.

If I comment out the offending line, I simply get an exception at the next variable.

And, as mentioned, all the data in the page is showing up so obviously the model object is not null...

What am I missing?

Upvotes: 3

Views: 20840

Answers (1)

Prasanna Kumar H A
Prasanna Kumar H A

Reputation: 3431

You are adding install attribute by checking null,if it's null then nothing will be initialized & then you are taking that in jsp th:text="${install.projectName}",so it's saying cannot be found on null.

So change to

InstallLinkJoined installLinkJoined = installLinkJoinedService.getInstallLinkWithID(installLink);
if (installLinkJoined != null) {
    model.addAttribute("install", installLinkJoined);
} else {
    model.addAttribute("install", new InstallLinkJoined());
}

Upvotes: 4

Related Questions