-1

i have the below code that everything runs fine except the check to see if the value is null. I know the value being returned is null, yet it still doesn't work. I've tried having == null both within and outside the {} to no avail.

Maybe it has something to do with how hibernate is returning the value? When i print out the object returned from the db it says null.

<div th:each="timecardLast: ${timecardLast}">
<a th:href="@{/timecardin}">
        <div th:if="${timecardLast.status == null}" style="width: 100%" class="waves-effect card-panel green darken-1 z-depth-4">
            <div class="card-content center-align">
                <i class="medium material-icons white-text">timer</i>
                <h5 class="white-text">SIGN IN TO WORK</h5>
            </div>
        </div>
    </a>
        <a th:href="@{/timecardin}">
        <div th:if="${timecardLast.status} == 1" style="width: 100%" class="waves-effect card-panel green darken-1 z-depth-4">
            <div class="card-content center-align">
                <i class="medium material-icons white-text">timer</i>
                <h5 class="white-text">SIGN IN TO WORK</h5>
            </div>
        </div>
    </a>
            <a th:href="@{/timecradin}">
        <div th:if="${timecardLast.status} == 2" style="width: 100%" class="waves-effect card-panel deep-orange darken-2 z-depth-4">
            <div class="card-content center-align">
                <i class="medium material-icons white-text">timer</i>
                <h5 class="white-text">SIGN IN TO WORK</h5>
            </div>
        </div>
    </a>
            <a th:href="@{/timecardout}">
        <div th:if="${timecardLast.status} == 0" style="width: 100%" class="waves-effect card-panel deep-orange darken-2 z-depth-4">
            <div class="card-content center-align">
                <i class="medium material-icons white-text">timer_off</i>
                <h5 class="white-text">SIGN OUT OF WORK</h5>
            </div>
        </div>
    </a>
    </div>
Marios
  • 130
  • 1
  • 13
  • Duplicate of https://stackoverflow.com/questions/44406091/thymeleaf-spel-check-null-values – TheOni Jan 24 '18 at 22:42
  • What prints when you have this on the page, outside of any if statements? `[[${timecardLast.status}]]` – riddle_me_this Jan 24 '18 at 22:42
  • The object itself was null so I changed it to query timecardLast == null but it's still not working. When I try to just print out the timecardLast object I get nothing on screen – Marios Jan 24 '18 at 23:31

2 Answers2

0

The syntax for lists in Thymeleaf is naming the element and naming the list. You have both names for the same thing. So you may want instead:

<div th:each="timecard: ${timecardList}">

...
    <div th:if="${timecard.status == null}"...>
...

After adding a list of objects called timecardList to the model.

riddle_me_this
  • 8,575
  • 10
  • 55
  • 80
  • No change regardless of name. When i print the object when it exists i get com.paphos.pos.timecard.Timecard@58b69afa. However, trying to print something that is null does not print anything on screen. – Marios Jan 25 '18 at 12:33
  • Also post your `Timecard` class. You should add a `toString()` method to it or use Lombok's helper methods. – riddle_me_this Jan 25 '18 at 15:08
0

Took a step back and looked at how i was creating the object. I was not initializing the object first i.e.

Timecard timecardLast = timecardService.getLastTimecardByIdusers(staff);

So, a simple initialization of the object properly did the job followed by the db request i.e.

timecardLast = new Timecard();
timecardLast = timecardService.getLastTimecardByIdusers(staff);
Marios
  • 130
  • 1
  • 13
  • If that's exactly what you have, that code doesn't make sense - you're just re-assigning `timecardLast`. You should take a look at what `getLastTimecardByIdusers(staff)` does and make sure that it returns a non-null object or handle it appropriately. If you update your question with that method, I can comment further in my answer. But the correct syntax for lists in Thymeleaf will still be needed. – riddle_me_this Jan 25 '18 at 15:02