Grails不能在null对象上获得属性'id' [英] Grails Cannot get property 'id' on null object

查看:107
本文介绍了Grails不能在null对象上获得属性'id'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是grails的新手,并且出现此错误:无法在空对象
上获取属性'id'我试图创建一个除添加新灯具外还显示灯具的Web应用程序。但是,在运行应用程序时,我无法添加灯具或显示处理添加灯具的页面,即add.gsp。以下是我的应用程序当前可用的类。



这是我简单的Fixture域类:

  package cricketfixturesgrailsapp 

class Fixture {
日期日期
字符串场地




// hasMany = [[scores:Scores ]

static hasMany = [team:Team]

static constraints = {
}
}



这是我的夹具控制器类:

 包cricketfixturesgrailsapp 
$ b 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 ){
[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 {
重定向动作:'show',id:fixtures [0]。 id
}
}
else {
[message:'fixtures.not.found']
}
}
}
}

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

 包cricketfixturesgrailsapp 

导入grails.transaction.Transactional

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

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

void updateFixture(夹具夹具,日期日期,字符串场地)
{
fixture.date =日期
fixture.venue =场地
fixture.save()
}

团队createTeam(字符串teamName1,字符串teamName2,long fixtureId){

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

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

}

formfield.gsp for表单

  $ {label}:< span class =errors>< g:fieldError bean =$ {bean}field =$ {field}/>< / span> 
< br />

另外我创建了3个gsp文件,添加一个fixture,找到一个fixture通过场地和显示灯具的节目,这里有3个gsp文件:

添加gsp

 <! -  
要更改此许可证标题,请在项目属性中选择许可证标题。

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

< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< title>添加灯具< / title>
< / head>

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

< table>
< tr>
< th>
< g:render template =/ common / formField
model =[name:'fixture',bean:fixtureBean,field:'date',label:'Date']/> ;
< / th>
< / tr>
< tr>
< th>
model =[name:'fixture',bean:fixtureBean,field:'venue',label:'Venue']/> ;
< / th>
< / tr>

< tr>
< td>
< p>< input type =submitvalue =$ {fixtureBean.id?'Update':'Add'} Fixture/>< / p>
< / td>
< / tr>
< / table>
< / g:表格>
< / body>
< / html>

< / body>
< / html>

find.gsp

 < HTML> 
< head>
< meta name =layoutcontent =main>
< title>查找灯具< / title>
< / head>

< body id =find>
< h2>查找灯具:< / h2>

< table>
< tr>
< th>
场地
< br />
< span class =errors>< g:message code =$ {message}>< / g:message>< / span>
< / th>
< / tr>
< tr>
< td>< p class =submit>< input type =submitvalue =Find fixtures/>< / p>< / td>
< / tr>
< / table>
< / g:表格>

< br />
< / body>
< / html>

show.gsp

 < HTML> 
< head>
< meta name =layoutcontent =main>
< title>夹具信息< / title>
< / head>

< body id =show>
< h2>夹具信息< / h2>

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

< / table>

< / body>
< / html>

另外我的默认主页是index.gsp,用户需要点击链接才能找到fixtures这反过来允许添加夹具

 <!DOCTYPE html> 

< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< title>样本标题< / title>
< / head>
< body>
< h2>< g:message code =welcome/>< / h2>

< ul>

< li>< g:link controller =Fixtureaction =add> Find fixture< / g:link>< / li>

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

stacktrace

  ....错误
|
2014-02-23 20:35:23,016 [http-bio-8080-exec-8] ERROR errors.GrailsExceptionResolver - 处理请求时发生NullPointerException:[GET] / CricketFixturesGrailsApp / fixture / add
Can not在null对象上获取属性id。 Stacktrace如下:
消息:评估表达式时出错[fixture.id? 'edit':'add'] on line [20]:无法获得null对象上的属性'id'
Line |方法
- >> 20 | doCall in C:/apache-tomcat-7.0.50/webapps/CricketFixturesGrailsApp/grails-app/views/fixture/add.gsp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
由NullPointerException引起:无法在空对象上获取属性'id'
- >> 44 | doCall in C__apache_tomcat_7_0_50_webapps_CricketFixturesGrailsApp_grails_app_views_fixture_add_gsp $ _run_closure2_closure8
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - $ - $ - $ - 47 |在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 | java.util.concurrent.ThreadPoolExecutor中的runWorker
| 603 |在java.util.concurrent.ThreadPoolExecutor $ Worker
^ 722 |中运行跑 。 。 。在java.lang.Thread中


解决方案



  def fixture = FixtureService.createFixture(params.fixture?.date,
)params.fixture?.venue)
if(!fixture.hasErrors()){
重定向动作:'show',id:fixture.id //在这里问题
返回
}

beans( FixtureService )应该注入为:

  def fixtureService //小写

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

I am new to grails and I am getting this error: Cannot get property 'id' on null object I am trying to create a web application that displays fixtures in addition to adding new fixtures. However, when running the application I am unable to add fixtures or display the page that deals with adding the fixtures i.e add.gsp. Here are the classes that are currently available in my application.

Here is my simple Fixture domain class:

package cricketfixturesgrailsapp

class Fixture {
   Date date
   String venue




 // hasMany  = [scores:Scores]

 static hasMany = [team:Team]

static constraints = {
}
}

here is my fixture controller class:

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']
            }
        }
    }
}

I've created a fixtureService to deal with creation and update of fixtures

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()
}

}

A formfield.gsp for the form

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

In addition I've created 3 gsp files, add to add a fixture, find, finds a fixture by venue and show which shows fixtures, here are the 3 gsp files:

Add 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>

find.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>

show.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>

Also my default home page is index.gsp, where users are expected to click a link to find fixtures which in turn allows a fixture to be added

<!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>

stacktrace

....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

解决方案

I believe you are getting the error from the below section of code.

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

beans(FixtureService in this case) should be injected as:

def fixtureService //lower case 

Hence, fixtureService is not injected the way it is used now. This is my anticipation until a full stacktrace of the error is added to the question. This (service class injection) has to be fixed anyways.

这篇关于Grails不能在null对象上获得属性'id'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆