调用服务时Grails Null Pointer异常

时间:2016-12-12 18:12:37

标签: grails

我正在尝试调用我在控制器中创建的服务。 这是服务:

package matriculation

import grails.transaction.Transactional
import groovy.sql.Sql

@Transactional
class StudNamesService {

    def getNameById(String id) {

        return id
    }
}

这是控制器的一部分

package matriculation

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

@Transactional(readOnly = true)
class MatrEntryController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def studNamesService

    def getNameById(String id){
        return studNamesService.getNameById(id)
    }
}

当我调用调用该服务的控制器内部的方法时,我收到以下错误:

Class: java.lang.NullPointerException
Message: Request processing failed; nested exception is org.grails.gsp.GroovyPagesException: Error evaluating expression [cont.getNameById(matrEntryInstance.peopleCodeId)] on line [61]: Cannot invoke method getNameById() on null object
Caused by:Cannot invoke method getNameById() on null object

我对此问题的代码进行了微小的更改。如您所见,控制器中的调用具有与服务名称完全相同的名称。我正在运行Grails 3.1.1

修改1 这是调用方法的GSP页面的一部分:

<%@ page import="matriculation.MatrEntryController" %>

<%
    def cont=grailsApplication.classLoader.loadClass("matriculation.MatrEntryController").newInstance()
%>
<!----------------------Skip a few lines----------------------------->
<table>
     <thead>
         <tr>
            <th id="cols">Options</th>
            <g:sortableColumn property="peopleCodeId" title="People Code ID" id="cols"/>
            <g:sortableColumn property="cadetName" title="Cadet Name" id="cols"/>
            <g:sortableColumn property="soff" title="SOFF" id="cols"/>
            <g:sortableColumn property="infirmary" title="Infirmary" id="cols"/>
            <g:sortableColumn property="hsRotc" title="HS ROTC" id="cols"/>
            <g:sortableColumn property="idfy" title="IDFY" id="cols"/>
            <g:sortableColumn property="pao" title="Public Affairs" id="cols"/>
            <g:sortableColumn property="busOffice" title="Business Office" id="cols"/>
            <g:sortableColumn property="telephone" title="Telephone" id="cols"/>
            <g:sortableColumn property="computer" title="Computer" id="cols"/>
            <g:sortableColumn property="athletics" title="Athletics" id="cols"/>
            <g:sortableColumn property="financialAid" title="Financial Aid" id="cols"/>
            <g:sortableColumn property="compass" title="Compass" id="cols"/>
            <g:sortableColumn property="regFlag" title="Registration Flag" id="cols"/>
            <g:sortableColumn property="busOfficeForm" title="${raw('Business<br/>Office Form')}" id="cols"/>
         </tr>
       </thead>
       <tbody>
         <g:each in="${matrEntryList}" status="i" var="matrEntryInstance">
             <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
                 <td id="cols">
                     <g:form resource="${matrEntryInstance}" method="DELETE">
                        <a href="${createLink(controller:'matrEntry',action:'show')+'/'+matrEntryInstance.id}" class="btn btn-default">Show</a>
                         <a href="${createLink(controller:'matrEntry',action:'edit')+'/'+matrEntryInstance.id}" class="btn btn-default">Edit</a>
                     </g:form>
                   </td>
                   <td id="cols">${matrEntryInstance.peopleCodeId}</td> 
                   <td id="cols">${cont.getNameById(matrEntryInstance.peopleCodeId)}</td>
                   <td id="cols">${matrEntryInstance.soff}</td>
                   <td id="cols">${matrEntryInstance.infirmary}</td>
                   <td id="cols">${matrEntryInstance.hsRotc}</td>
                   <td id="cols">${matrEntryInstance.idfy}</td>
                   <td id="cols">${matrEntryInstance.pao}</td>
                   <td id="cols">${matrEntryInstance.busOffice}</td>
                   <td id="cols">${matrEntryInstance.telephone}</td>
                   <td id="cols">${matrEntryInstance.computer}</td>
                   <td id="cols">${matrEntryInstance.athletics}</td>
                   <td id="cols">${matrEntryInstance.financialAid}</td>
                   <td id="cols">${matrEntryInstance.compass}</td>
                   <td id="cols"><g:formatDate date="${matrEntryInstance.regFlag}" format="dd MMM, yyyy"/></td>
                   <td id="cols">${matrEntryInstance.busOfficeForm}</td>
                 </tr>
               </g:each>
             </tbody>
          </table>

1 个答案:

答案 0 :(得分:2)

这里的问题是你正在以错误的方式解决问题。您应在GSP页面中创建控制器实例。因为您要做的是调用服务,您应该真正使用标记库来查看。

以下是标记库的一个简单示例,它可以在您的GSP中使用:

package com.example

class ExampleTagLib {
    static namespace = "myLookup"
    def exampleService

    def lookupName = { attrs, body ->
        if (!attrs.id) return
        out << exampleService.getNameById(attrs.id)
    }
}

在您的GSP中使用如下所示:

<td id="cols">${myLookup.lookupName(id: matrEntryInstance.peopleCodeId)}</td>

我强烈建议您在尝试之前阅读documentation来创建标记库。