错误:尝试在空上下文中调用方法 isEmpty

Posted

技术标签:

【中文标题】错误:尝试在空上下文中调用方法 isEmpty【英文标题】:Error: Attempted to call method isEmpty on null context 【发布时间】:2019-08-25 14:06:49 【问题描述】:

我有这个 html/thymleaf 模板,它一直在使用 th:if 语句。该代码使用 if 语句仅显示 div 及其内容,前提是 SQL 数据库中的该字段中有内容。

我正在尝试添加一个新的私有字符串cyberAttackInsurance 并使用相同的代码来显示它,但它抛出了这个错误:方法调用:尝试在空上下文对象上调用方法isEmpty()

为什么这部分代码有问题,而其他代码没有问题?我尝试使用 th:block 和 th:switch th:case 以不同的方式编写它,但它也不喜欢那样......这引发了不同的错误。任何关于如何修复此错误的想法或更深入地了解为什么会发生这种情况将不胜感激!

这是添加了cyberAttackInsurance的代码。

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorator="fragments/layout" th:with="currentPage='dashboard'">
<head>
    <title>Home</title>
</head>
<body>
<div  class="container" layout:fragment="content">
<div class="row">
    <br/>

    <div class="ui segment">
        <div class="ui container">
            <div th:if="$param.sentMessageFail">
                <div class="ui warning message">
                    <p style="color: #ffffff; text-align:left;"> Oops! Something went wrong. Please try submitting your form again.</p>
                </div>
            </div>
        </div>
        <div th:if="$param.sentMessage">
            <div class="ui positive message">
                <p style="color: #ffffff; text-align:left;"> Sent Successfully!</p>
            </div>
        </div>
    </div>

        <div class="col m6 xl3 valign-wrapper">
            <div th:if="$access!=null and !access.micrositeLink.isEmpty()" class="card xs scale-transition scale-out z-depth-0">
                <a th:href="$access.micrositeLink" target="_blank">
                    <div class="card-image">
                        <img src="images/ReferralMicrosite.jpg"/>
                    </div>
                </a>
                <div class="card-content">
                    <p>Submit a new opportunity</p>
                </div>
            </div>
        </div>
        <div class="col m6 xl3 valign-wrapper">
            <div th:if="$access!=null and !access.partnerPortalLink.isEmpty()" class="card xs scale-transition scale-out z-depth-0">
                <a th:href="$access.partnerPortalLink" target="_blank" style="background-color:#2f373e;color:white;">
                    <div class="card-image">
                        <img src="images/ProspectManager.png"/>
                    </div>
                </a>
                <div class="card-content">
                    <p>Track all your referral business</p>
                </div>
            </div>
        </div>

        <div class="col m6 xl3 valign-wrapper">
            <div  th:if="$#authorization.expression('hasRole(''ROLE_ADMIN'')') or $loggedUser.client.ams360Affiliate == 'All Access – Traditional'" class="card xs scale-transition scale-out z-depth-0">
                <a href="https://platform.vertafore.com/#/" target="_blank">
                <div class="card-image">
                    <img src="images/ams360.png"/></div>
                </a>
                <div class="card-content">
                    <p>Agency Management System</p>
                </div>
            </div>
        </div>
        <div class="col m6 xl3 valign-wrapper">
            <div  th:if="$#authorization.expression('hasRole(''ROLE_ADMIN'')') or $loggedUser.client.ams360Affiliate == 'All Access – Traditional'" class="card xs scale-transition scale-out z-depth-0">
                <a href="https://rating.vertafore.com/UserInterface/main/login.aspx" target="_blank">
                    <div class="card-image">
                        <img src="images/plRating.png"/>
                    </div>
                </a>
                <div class="card-content">
                    <p>Personal Lines Comparative Rater &amp; Quoting</p>
                </div>
            </div>
        </div>
    </div>
    <div class="row flex" style="margin-bottom:0;">
        <div class="col s12 xl6">
            <div class="row" style="margin-bottom:-5px;">
                <div class="col s6 m4 scale-transition scale-out">
                    <a th:href="@/carriers">
                        <div class="white red-text text-accent-2 valign-wrapper ">
                        <span class="center-align" style="width:100%; padding:10px;">
                            <img src="/images/menu_icons/icons8-handshake-48.png"/>
                            <br/>
                            <span class="grey-text text-darken-2">Know your carriers!</span>
                        </span>
                        </div>
                    </a>
                </div>
                <div th:if="$access!=null and !access.indioCustomerApplicationSite.isEmpty()" class="col s6 m4 scale-transition scale-out">
                    <a th:href="$access.indioCustomerApplicationSite" target="_blank">
                        <div class="white text-accent-4 valign-wrapper ">
                             <span class="center-align" style="width:100%; padding:10px;">
                                 <img class="responsive-img" src="images/Indio.png"/>
                                <br/>
                                 <span class="grey-text text-darken-2">Online Applications for your customers to complete</span>
                             </span>
                        </div>
                    </a>
                </div>
                <div class="col s6 m4 scale-transition scale-out">
                    <a target="_blank" href="https://www.university.com/">
                        <div class="white valign-wrapper ">
                        <span class="center-align" style="width:100%; padding:10px;">
                            <img src="/images/menu_icons/icons8-reading-48.png"/>
                            <br/>
                            <span class="grey-text text-darken-2">University</span>
                        </span>
                        </div>
                    </a>
                </div>

                <div th:if="$access!=null and !access.cyberAttackInsurance.isEmpty()" class="col s6 m4 scale-transition scale-out">
                    <a th:href="$access.cyberAttackInsurance" target="_blank">
                        <div class="white valign-wrapper ">
                        <span class="center-align" style="width:100%; padding:10px;">
                                 <img class="responsive-img" src="images/vendor_logos/CAI.jpg"/>
                            <br/>
                            <span class="grey-text text-darken-2">Cyber Attack Insurance</span>
                        </span>
                        </div>
                    </a>
                </div>
                <div th:if="$access!=null and !access.hiscoxQuotingSite.isEmpty()" class="col s6 m4 scale-transition scale-out">
                    <a th:href="$access.hiscoxQuotingSite" target="_blank">
                        <div class="white black-text text-accent-4 valign-wrapper ">
                             <span class="center-align" style="width:100%; padding:10px;">
                                 <img class="responsive-img" src="images/vendor_logos/hiscox_logo.gif"/>
                                <br/>
                                 <span class="grey-text text-darken-2">Online Quoting site for <strong>P&amp;C licensed Affiliates only</strong></span>
                             </span>
                        </div>
                    </a>
                </div>
                <div th:if="$access!=null and !access.CRQLink.isEmpty()"  class="col s6 m4 scale-transition scale-out">
                    <a th:href="https://esubmissions.thehartford.com/" target="_blank">
                        <div class="white black-text valign-wrapper " >
                                    <span class="center-align" style="width:100%; padding:10px;">
                                        <img class="responsive-img" src="images/hartford-logo.png"/>
                                        <br/>
                                        <span class="grey-text text-darken-2">The Hartford Pocket </span>
                                    </span>
                        </div>
                    </a>
                </div>
                <div th:if="$access!=null and !access.hartfordCustomerQuotingSite.isEmpty()" class="col s6 m4 scale-transition scale-out">
                    <a th:href="$'mailto:?body=Thank you for your interest in our Commercial Lines Insurance, here is a link to get a quote online NOW! '+access.hartfordCustomerQuotingSite+'&amp;subject=Thank you for your interest!'" >
                        <div class="white black-text valign-wrapper" >
                                    <span class="center-align" style="width:100%; padding:10px;">
                                        <i class="material-icons center-align" style="font-size:3.25rem">mail_outline</i>
                                        <br/>
                                        <span class="grey-text text-darken-2">Email: Link to Hartford Customer Facing Rater </span>
                                    </span>
                        </div>
                    </a>
                </div>
            </div>
        </div>
        <div class="col s12 xl6">
            <div class="white scale-transition scale-out" style="padding:5px 10px">
                <h6><Strong>Latest Announcements</Strong></h6>
                <table class="table striped" data-toggle="table" data-classes="table-no-bordered" data-striped="true" data-pagination="true" data-page-size="3">
                    <thead>
                        <tr>
                            <th></th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                         <tr th:each="announcement : $announcements">
                                <td>
                                    <a th:href="@|/announcement/$announcement.id|">
                                    <h6><strong th:text="$announcement.heading">Announcement Heading</strong></h6>
                                        <small class="designation" th:text="$announcement.description">Description for this announcement.</small></a>
                                </td>
                                <td class="center-align">
                                    <h6><strong th:if="$announcement.createdDate" th:text="$#dates.day(announcement.createdDate)">28</strong></h6>
                                    <h6><strong th:if="$announcement.createdDate" th:text="$#dates.monthName(announcement.createdDate)">Feb</strong></h6>
                                </td>
                         </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col s12">
            <div class="white scale-transition scale-out " style="padding:5px 10px">
                <h6 style="margin-bottom:-40px;"><strong>Resources Directory</strong></h6>
                <table class="table striped highlight" data-toggle="table" data-classes="table-no-bordered" data-striped="true" data-search="true" data-pagination="true" data-page-size="4">
                    <thead>
                    <tr>
                        <th>Name</th>
                        <th>Category</th>
                        <th>Services</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr th:each="resource : $resources">
                        <td><a th:href="@/" th:text="$resource.name"></a></td>
                        <td th:text="$resource.category"></td>
                        <td th:text="$resource.services"></td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    <script>
        $(document).ready(function()
            $('.scale-out').addClass("scale-in");
        );
    </script>
    </div>
</div>
</body>
</html>

这是模型:

@Entity
public class ResourceWebsiteAccess 

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ResourceWebsiteAccessId")
    private Long id;
    private String micrositeLink;
    private String partnerPortalLink;
    private String hartfordCustomerQuotingSite;
    private String hiscoxQuotingSite;
    private String indioCustomerApplicationSite;
    private String CRQLink;
    private String cyberAttackInsurance;
    @OneToOne
    @JoinColumn(name = "client")
    private Client client;

    public Long getId() 
        return id;
    

    public void setId(Long id) 
        this.id = id;
    

    public String getMicrositeLink() 
        return micrositeLink;
    

    public void setMicrositeLink(String micrositeLink) 
        this.micrositeLink = micrositeLink;
    

    public String getPartnerPortalLink() 
        return partnerPortalLink;
    

    public void setPartnerPortalLink(String partnerPortalLink) 
        this.partnerPortalLink = partnerPortalLink;
    

    public Client getClient() 
        return client;
    

    public void setClient(Client client) 
        this.client = client;
    

    public String getHartfordCustomerQuotingSite() 
        return hartfordCustomerQuotingSite;
    

    public void setHartfordCustomerQuotingSite(String hartfordCustomerQuotingSite) 
        this.hartfordCustomerQuotingSite = hartfordCustomerQuotingSite;
    

    public String getHiscoxQuotingSite() 
        return hiscoxQuotingSite;
    

    public void setHiscoxQuotingSite(String hiscoxQuotingSite) 
        this.hiscoxQuotingSite = hiscoxQuotingSite;
    

    public String getIndioCustomerApplicationSite() 
        return indioCustomerApplicationSite;
    

    public void setIndioCustomerApplicationSite(String indioCustomerApplicationSite) 
        this.indioCustomerApplicationSite = indioCustomerApplicationSite;
    

    public String getCRQLink() 
        return CRQLink;
    

    public void setCRQLink(String CRQLink) 
        this.CRQLink = CRQLink;
    

    public String getCyberAttackInsurance() 
        return cyberAttackInsurance;
    

    public void setCyberAttackInsurance(String cyberAttackInsurance) 
        this.cyberAttackInsurance = cyberAttackInsurance;
    

【问题讨论】:

【参考方案1】:

首先检查cyberAttackInsurance 是否为空,就像为access 做的一样

<div th:if="$access!=null and access.cyberAttackInsurance != null and  !access.cyberAttackInsurance.isEmpty()" class="col s6 m4 scale-transition scale-out">

更好的是你可以简单地使用条件运算符

<div th:if="$access?.cyberAttackInsurance" class="col...

【讨论】:

【参考方案2】:

你忘了设置cyberAttackInsurance 吗?错误是因为您调用的是isEmpty(),但它是null,因此您在这里看到一个空指针。

【讨论】:

无论cyberAttackInsurance 中是否有值,我都希望页面加载。我只是不希望带有cyberAttackInsurance 信息的div/box 显示给在cyberAttackInsurance 字段中没有值的用户。 在这种情况下,您需要在调用isEmpty()之前添加一个不为空的检查

以上是关于错误:尝试在空上下文中调用方法 isEmpty的主要内容,如果未能解决你的问题,请参考以下文章

阿波罗空上下文

在 android 中使用 Telr 支付网关时上下文为空

缺少 String.IsEmpty 方法

在此上下文中无法调用监视窗口中的方法评估

iOS Swift 3核心数据-尝试递归调用-save的致命错误:上下文中止

通过调用Local Method响应Vue.js事件总线时丢失上下文