Grails 无法获取 null 对象的属性 'id'

标签 grails groovy gsp

我是 grails 新手,但收到此错误:无法在 null 对象上获取属性“id” 我正在尝试创建一个 Web 应用程序,除了添加新的灯具之外,还可以显示灯具。但是,在运行应用程序时,我无法添加灯具或显示处理添加灯具的页面,即 add.gsp。以下是我的应用程序中当前可用的类(class)。

这是我的简单 Fixture 域类:

package cricketfixturesgrailsapp

class Fixture {
   Date date
   String venue




 // hasMany  = [scores:Scores]

 static hasMany = [team:Team]

static constraints = {
}
}

这是我的夹具 Controller 类:

package cricketfixturesgrailsapp

class FixtureController {

 //def index() { }
    def FixtureService


    def add() {
        if (request.method == 'GET') {
            [fixtureBean: new Fixture()]
        }
        else {
            def fixture = FixtureService.createFixture(params.fixture?.date, params.fixture?.venue)
            if (!fixture.hasErrors()) {
                redirect action: 'show', id: fixture.id
                return
            }

            [fixtureBean: fixture]
        }
    }

  def show() {
        def fixture = Fixture.get(params.id)
        if (fixture) {
            [fixtureBean: fixture]
        }

    }
            def find() {
        if (request.method == 'POST') {
            def fixtures = Fixture.findAllByDate(params.date)
            if (fixtures) {
                if (fixtures.size() > 1) {
                    render view: 'selection', model: [fixtures: fixtures]
                }
                else {
                    redirect action: 'show', id: fixtures[0].id
                }
            }
            else {
                [message: 'fixtures.not.found']
            }
        }
    }
}

我创建了一个fixtureService来处理fixture的创建和更新

package cricketfixturesgrailsapp

import grails.transaction.Transactional

//@Transactional
class FixtureService {
//def serviceMethod() {

  Fixture createFixture(Date date, String venue)
    {
        def fixture = new Fixture(date: date, venue:venue)
        fixture.save()
        fixture
    }

void updateFixture(Fixture fixture,Date date, String venue)
{
    fixture.date = date
    fixture.venue = venue
    fixture.save()
}

Team createTeam(String teamName1, String teamName2, long fixtureId){

    def team = new Team(teamName1: teamName1, teamName2: teamName2, fixture: Fixture.load(fixtureId))
    team.save()
    team

}
void updateTeam(String teamName1, String teamName2, long fixtureId) {
    team.teamName1 = teamName1
    team.teamName2 = teamName2
    team.fixture = Fixture.load(fixtureId)
    team.save()
}

}

表单的 formfield.gsp

${label}: <span class="errors"><g:fieldError bean="${bean}" field="${field}" /></span>
<br/>
<g:textField name="${name + '.' +field}" value="${fieldValue(bean:bean, field:field)}" />

此外,我还创建了 3 个 gsp 文件,添加添加灯具、查找、按 field 查找灯具并显示其中显示的灯具,这是 3 个 gsp 文件:

添加gsp

<!--
  To change this license header, choose License Headers in Project Properties.

<%@ page contentType="text/html;charset=UTF-8" %>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Add fixture</title>
    </head>

    <body>
      <h2><g:if test="${!fixtureBean.id}">New </g:if>Fixture:</h2>

      <g:form action="${ fixture.id ? 'edit' : 'add'}" id="${fixtureBean?.id}">
            <table>
                <tr>
                    <th>
                        <g:render template="/common/formField"
                                  model="[name:'fixture', bean:fixtureBean, field:'date', label:'Date']" />
                    </th>
                </tr>
                <tr>
                    <th>
                        <g:render template="/common/formField"
                                  model="[name:'fixture', bean:fixtureBean, field:'venue', label:'Venue']" />
                    </th>
                </tr>

                <tr>
                    <td>
                        <p><input type="submit" value="${ fixtureBean.id ? 'Update' : 'Add'} Fixture"/></p>
                    </td>
                </tr>
            </table>
        </g:form>
    </body>
</html>

    </body>
</html>

查找.gsp

<html>
    <head>
        <meta name="layout" content="main">
        <title>Find fixtures</title>
    </head>

    <body id="find">
        <h2>Find fixtures:</h2>

        <g:form action="find">
            <table>
                <tr>
                    <th>
                        venue
                        <br/>
                        <g:textField name="venue"/>
                    <span class="errors"><g:message code="${message}"></g:message></span>
                    </th>
                </tr>
                <tr>
                    <td><p class="submit"><input type="submit" value="Find fixtures"/></p></td>
                </tr>
            </table>
        </g:form>

        <br/>
        <g:link controller="Fixture" action="add">Add fixture</g:link></a>
    </body>
</html>

显示.gsp

<html>
    <head>
        <meta name="layout" content="main">
        <title>fixture Information</title>
    </head>

    <body id="show">
        <h2>fixture Information</h2>

            <table>
                <tr>
                    <th>Date</th>
                    <td><b>${fixtureBean.date} ${fixtureBean.venue}</b></td>
                </tr>

            </table>

    </body>
</html>

我的默认主页是index.gsp,用户需要单击链接来查找灯具,从而允许添加灯具

<!DOCTYPE html>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Sample title</title>
    </head>
    <body>
      <h2><g:message code="welcome"/></h2>

        <ul>

                        <li><g:link controller="Fixture" action="add">Find fixture</g:link></li>

        </ul>
    </body>
</html>

堆栈跟踪

....Error 
|
2014-02-23 20:35:23,016 [http-bio-8080-exec-8] ERROR errors.GrailsExceptionResolver  - NullPointerException occurred when processing request: [GET] /CricketFixturesGrailsApp/fixture/add
Cannot get property 'id' on null object. Stacktrace follows:
Message: Error evaluating expression [fixture.id ? 'edit' : 'add'] on line [20]: Cannot get property 'id' on null object
    Line | Method
->>   20 | doCall    in C:/apache-tomcat-7.0.50/webapps/CricketFixturesGrailsApp/grails-app/views/fixture/add.gsp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Caused by NullPointerException: Cannot get property 'id' on null object
->>   44 | doCall    in C__apache_tomcat_7_0_50_webapps_CricketFixturesGrailsApp_grails_app_views_fixture_add_gsp$_run_closure2_closure8
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     47 | run       in C__apache_tomcat_7_0_50_webapps_CricketFixturesGrailsApp_grails_app_views_fixture_add_gsp
|    200 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run . . . in java.lang.Thread

最佳答案

我相信您从下面的代码部分中收到了错误。

def fixture = FixtureService.createFixture(params.fixture?.date, 
                                           params.fixture?.venue)
if (!fixture.hasErrors()) {
    redirect action: 'show', id: fixture.id //issue here
    return
}

beans(在本例中为FixtureService)应注入(inject)为:

def fixtureService //lower case 

因此,fixtureService 并未按照现在使用的方式注入(inject)。这是我的预期,直到将错误的完整堆栈跟踪添加到问题中。无论如何,这个(服务类注入(inject))必须被修复。

关于Grails 无法获取 null 对象的属性 'id',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21973247/

相关文章:

javascript - 我如何从 JavaScript 和 Grails 访问变量?

mongodb - 在MongoDB中计算距离不准确

grails - 如何将 grails 3 插件发布到我的本地 nexus 存储库?

grails - 如何通过 Grails Fields Plugin 使用 transient 域属性

linux - 以编程方式重启 Grails 应用程序

grails - Grails:如何在gsp或使用js动态设置语言

grails - Grails使用命令对象和同一gsp中的对象

mysql - grails/hibernate 是否在 PostgreSQL 中存储以毫秒为单位的日期?

grails - 订单不符合 Grails 中的标准

grails - Grails如何根据变化的搜索参数动态搜索