2014-02-23 2 views
2

Grails에 익숙하지 않아이 오류가 발생합니다. null 객체에 'id'속성을 가져올 수 없습니다. 추가하는 것 외에 조명기를 표시하는 웹 응용 프로그램을 만들려고합니다. 새로운 설비. 그러나, 응용 프로그램을 실행할 때 조명기를 추가하거나 조명기 추가 (add.gsp) 페이지를 표시 할 수 없습니다. 내 응용 프로그램에서 현재 사용할 수있는 클래스는 다음과 같습니다. 여기 Grails null 객체에 'id'속성을 가져올 수 없습니다.

package cricketfixturesgrailsapp 

class Fixture { 
    Date date 
    String venue 




// hasMany = [scores:Scores] 

static hasMany = [team:Team] 

static constraints = { 
} 
} 

내 고정 컨트롤러 클래스입니다 : 여기

내 간단한 고정 장치 도메인 클래스 I 일정의 생성 및 업데이트를 처리하는 fixtureService을 만든

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

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

} 

양식 필드의 양식 필드는

입니다. 내가 3 개 GSP 파일, 고정물을 추가 찾기에 추가를 만들었습니다 또한 17,451,515,
${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 파일은 다음과 같습니다

는 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

,

또한 내 기본 홈 페이지는 사용자가 차례로 고정이

....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.gsp'에'add '라고 쓰여진 답안에 언급 된 수정 사항 이외에? @Sky – dmahapatro

+0

감사합니다. 제안 된 변경 사항을 적용한 후에 작동합니다. – Sky

0

여기

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

스택 트레이스를 추가 할 수 있습니다 비품을 찾기 위해 링크를 클릭 할 것으로 예상되는 경우,의 index.gsp입니다 내가 무엇을 할 것인가 : 일단 도메인이 있으면 컨트롤러와 gsp 페이지도 생성합니다. 그게 무엇을 제공할까요?

먼저 "scaffold = true"대신 실제 코드가있는 컨트롤러를 갖게됩니다. How to generate

둘째 당신은/수정 작업을 점차적으로 각 단계를 확인하는 행동 &를 변경하는 컨트롤러와 GSP 페이지에 기능을 추가 할 수 있습니다.

+0

나는이 모든 작업을 수행하고 etcc 파일을 추가했지만 오류가 발생하는 이유를 모르겠습니다. – Sky

+0

dmahapatro가 맞습니다 - 전체 스택 추적을 추가하십시오. – MeIr

관련 문제