2014-02-23 81 views
2

我是grails的新手,但出現此錯誤:無法在空對象上獲取屬性'id' 我正在嘗試創建一個Web應用程序,新的裝置。但是,在運行應用程序時,我無法添加燈具或顯示處理添加燈具的頁面,即add.gsp。以下是我的應用程序當前可用的類。Grails無法在空對象上獲取屬性'id'

這裏是我的簡單的燈具域類:

package cricketfixturesgrailsapp 

class Fixture { 
    Date date 
    String venue 




// hasMany = [scores:Scores] 

static hasMany = [team:Team] 

static constraints = { 
} 
} 

這裏是我的燈具控制器類:

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應對燈具的創建和更新

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文件,新增加一個夾具,發現,通過場地和演出這說明燈具找到一個夾具,這裏有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> 

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> 

而且我的默認主頁是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 

回答

5

我相信你從下面的代碼部分得到的錯誤。

def fixtureService //lower case 

因此,fixtureService不注入現在的使用方式:作爲

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

豆(FixtureService在這種情況下)應注射。這是我的期待,直到錯誤的完整堆棧跟蹤被添加到問題中。這(服務類注入)必須被修復。

+0

我添加了完整的stactrace – Sky

+0

你試過'fixtureBean.id? 'edit':'add'' in add.gsp'除了在回答中提到的修復? @Sky – dmahapatro

+0

謝謝,它進行了你所建議的更改後 – Sky

0

這裏是我會做的: 一旦你有了域,我也會生成控制器和gsp頁面。那會提供什麼?

首先,您將擁有一個實際代碼而不是「scaffold = true」的控制器。 How to generate

其次,你將能夠修改/到控制器和GSP頁面所工作,逐漸改變人們的行爲&驗證每一步添加功能。

+0

我已經完成了所有這些,添加了gsp文件etcc,但我不知道爲什麼我得到錯誤。 – Sky

+0

dmahapatro是正確的 - 請添加完整的堆棧跟蹤。 – MeIr

相關問題