EL1007E: Property or field 'fieldName' cannot be found on null
Asked Answered
H

2

7

Good evenning i no longer have a solution ..Ive been hesitating to ask for help but Im litteraly at a dead end . Im working on a Spring boot 2.0.5 Spring MVC 5.0.9, ThymeLeaf 3.0.9 project that needs to be delievered in few weeks ..I have encountered a problem for some weeks now ...did my research and tried every possible solution and I still have the same problem . In Fact, my controller does not bind my model variables to my view ...it always renders "EL1007E: Property or field 'fieldName' cannot be found on null" .. Ive litteraly tried everything(since my code is just fine )..

  1. upgrading and downgrading JDK/JRE and matching them with eclipse version worked once,got me the information i needed but then got the same issue back .
  2. Using ModelAndView instead of String for rendreding web pages

  3. mvn clean/mvn install /mvn dependency:resolve ...every command i found helpfull

  4. deleting the ./m2 repository for unnecessary dependencies
  5. even setting a new workspace ..compiling debugging and im really stuck .. can you give me some advice please !!

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
	data-layout-decorate="~{index}">
<head>
<meta charset="UTF-8" />
<title>Page test</title>
</head>
<body>
	<div data-layout-fragment="content">
		<div class="container">
			<div class="row">
				<div class="col-sm-6" align="center">

					<form th:action="@{/consulter}" method="get"
						th:object="${paramSociete}">
						<input type="text" name="nomSociete" class="form-control"
							placeholder="AMEN BANK" />
						<button type="submit">Clique moi !!</button>
					</form>
					<br /> <br /> <br /> <br />
					<div>
						<div>
							<label>Nom Banque :</label><Label th:inline="text">
								[[${paramSociete.nomSociete}]]</Label>
						</div>
						<div>
							<label>Reference msg:</Label><Label th:inline="text">[[${paramSociete.initialMsg}]]</Label>
						</div>
						<div>
							<label>chemin dacces:</label> <Label th:inline="text">[[${paramSociete.pathMsgEmis}]]</Label>

						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</body>
</html>
@Controller
public class ParamSocieteController {
    @Autowired
    private ParamSocieteServiceImpl societeServiceImpl;

    @Autowired
    public void setSocieteServiceImpl(ParamSocieteServiceImpl societeServiceImpl) {
        this.societeServiceImpl = societeServiceImpl;
    }

    @RequestMapping(value="/")
    public String showTest() {

        System.out.println("Here we go !!");
        return "ThymeTest";

    }

    @RequestMapping(value = "/consulter")
    public String afficherAmenBank(Model model, String nomSociete) {
        ParamSociete societe = societeServiceImpl.findSociete(nomSociete);
        if (societe == null) {
            model.addAttribute("paramSociete", new ParamSociete());
        } else {
            model.addAttribute("nomSociete", nomSociete);
            model.addAttribute("paramSociete", societe);
            System.out.println(societe.getNomSociete());
            System.out.println(societeServiceImpl.findSociete(societe.getNomSociete()).toString());
        }
        return "ThymeTest";
    }
}

so i did nothing in my controller but did this in my view : I tested if my object existed with th:if

<div th:if="${paramSociete}">
                    <div>
                        <label>Nom Banque :</label><Label th:inline="text">
                            [[${paramSociete.nomSociete}]]</Label>
                    </div>
                    <div>
                        <label>Reference msg:</Label><Label th:inline="text">[[${paramSociete.initialMsg}]]</Label>
                    </div>
                    <div>
                        <label>chemin dacces:</label> <Label th:inline="text">[[${paramSociete.pathMsgEmis}]]</Label>

                    </div>
                </div>
Heliacal answered 24/11, 2018 at 22:16 Comment(8)
If your code causes an error to be thrown, it's not "just fine". It has a bug that needs to be fixed. It's in your code, not in the tools you're using. So, analyze the error message. See what causes the error to be thrown. Analyze your code to understand why it's thrown, and fix the code. You've literally tried everything, except that.Ketone
If you want help, or if you want to find out the bug by yourself, as I already said, the very first step is to analyze the error message and stack trace. The complete and exact error message and stack trace. Telling what and where the problem is. You haven't posted this crucial piece of information.Ketone
Thank you for your kind answer ive just updated my question ...ive tested my methods..my thymeleaf as well and they both work separtly "just fine" so ..let me know if you are willing to helpHeliacal
You also need to tell what you're doing. Which URL are you visiting, for example, i.e. what is the URL in the browser location bar when the bug happens?Ketone
ERROR 7536 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/ThymeTest.html]")] with root cause org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'nomSociete' cannot be found on nullHeliacal
Good. Now we're moving in the right direction. Now what is the URL in the browser location bar when this bug happens?Ketone
when i hit localhost:8080 in my browser i get that ERROR 7536 in my stack trace and whitelabel Error page in my browserHeliacal
not much just when i try "/" in my browser I get this : There was an unexpected error (type=Internal Server Error, status=500). An error happened during template parsing (template: "class path resource [templates/ThymeTest.html]")Heliacal
K
5

OK. So the problem is dead simple. Your view has this line of code:

${paramSociete.nomSociete}

So it tries to display the property nomSociete of the model attribute paramSociete. The error message tells you

Property or field 'nomSociete' cannot be found on null 

So paramSociete is null. Which means that there is no such model attribute. Let's check. Have you added such an attribute in the model before displaying that page? The method of your controller which is mapped to the URL in the browser location bar only has

@RequestMapping(value="/")
public String showTest() {
    System.out.println("Here we go !!");
    return "ThymeTest";
}

So it displays your view, but no, there is no attribute at all in the model. Which explains that paramSociete is null.

As simple as that. If you want a page to display the name of a company, that company must exist.

Ketone answered 24/11, 2018 at 22:59 Comment(11)
Thank you very much for taking time to answer my question ..can i kindly ask you for one more favor lets move this conversation to chat please ..it will be easier for me to make my self look dumb over there ^^Heliacal
If you want. But I don't even know how to start a chat. I never use it.Ketone
Ironically only youo could do it..i have only one reputation cant dmove it to chat ...so if you want to , you can head to the first comments above your answer ,try adding a new comment and you will see this Please avoid extended discussions in comments. Would you like to automatically move this discussion to chat?Heliacal
Nope, sorry. Ask your question here.Ketone
Alors, like i said data already exists in data base ..my method retreiving that unique data from data base works but when i hit link i get this error ERROR 7536 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: La valeur est nulle] with root cause java.lang.RuntimeException: La valeur est nulle .Heliacal
If the URL is /consulter, how could Spring guess what is the name of the company that you want to display? You need to pass it, either as a path variable (/consulter/some-company-name), or as a request parameter (/consulter?company=some-company-name). And you of course need to configure the path and/or use the appropriate annotations. docs.spring.io/spring/docs/current/spring-framework-reference/…Ketone
Thank you once more for taking this amount of time to answer my stupide questions . Bien CordialementHeliacal
Those aren't stupid questions. But what you need to remember for this is that, in 99.9999% of the cases, especially when you're a newbie, if a bug happens, it's in your code. Not in the libraries used by millions of people. And that fixing a bug always starts by reading the error message and stack trace, analysing the code, and proceeing in a rational way. Not by trying random things. If this answer solved your problem, you're supposed to mark it as such with the check at the left of the answer.Ketone
Unfortunatly it didn't ..Im sure the bug comes from the way my controller is handling my view ..like you suggested in the comments..my code and my URL are not behaving correctly ..Im more than gratefull for enlightening me .. I will be glad to check it though ! ^_^Heliacal
You're using the same view to display the form allowing to search for a company, and to display the result of that search. Use two different views. Or, if you use the same view, check if the results exist before trying to display them: the result don't exist if you're just displaying the form for the first time.Ketone
yes exactly !I figured that out when you told me to use the appropriate annotations ..and i checked it again and its retrieving data and rendering the right view .. THANK YOU SO MUCH SîrHeliacal
M
0

${paramSociete?.nomSociete} with the ? you can make your expression nullsafe which is very usefull when you need to load the page even when the object is null. Hope this help :)

For reference check this https://mcmap.net/q/188428/-using-thymeleaf-when-the-value-is-null

Maillol answered 11/7, 2023 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.