2016-08-26 73 views
0

當我嘗試保存表單詳細信息時,出現錯誤。 (當我點擊從我的dataentry.gsp文件提交按鈕) 我認爲錯誤發生在我的控制器,我打電話的服務。............錯誤| java.lang.NullPointerException:無法在空對象上調用方法saveDataentryDetails()

Error | java.lang.NullPointerException: Cannot invoke method saveDataentryDetails() on null object 

這裏是我的dataentry.gsp

<%@ page contentType="text/html;charset=UTF-8" %> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/> 
<meta name="layout" content="main"/> 
<title>Data Entry</title> 
</head> 
<body> 
<div id="saveMsgDiv"></div> 
<g:formRemote name="dataEntryForm" update="saveMsgDiv" url="[controller: 'DataEntry', action:'saveAjax']"> 

    <table align="center"> 
      <tr> 
      <td> 
       <label for='employeeId'>EmployeeID:</label> 
      </td> 
      <td> 
       <g:textField name='employee_id' required id='employee_id'/> 
      </td> 
      </tr> 

      <tr> 
      <td> 
       <label for='team'>Team:</label> 
      </td> 
      <td> 
       <g:select name='team' required id='team' from="${['Java', 'QA', 'Database']}" 
       noSelection="['':'---select---']"/> 
      </td> 
      </tr> 

      <tr> 
       <td> 
       <label for='receiptDate'>Receipt Date:</label> 
       </td> 
       <td> 
       <g:textField name='receipt_dt' required id='receipt_dt'/> 
       </td> 
      </tr> 

      <tr> 
       <td> 
       <label for='restaurantName'>Restaurant Name:</label> 
       </td> 
       <td> 
       <g:textField name='restaurant_name' required id='restaurant_name'/> 
       </td> 
      </tr> 
      <tr> 
      <td> 
       <label for='numberOfPersons'>Number of Persons:</label> 
       </td> 
       <td> 
       <g:textField name='number_of_persons' required id='number_of_persons' maxlength="3"/> 
      </td> 
      </tr> 
      <tr> 
      <td> 
       <label for='amount'>Amount:</label> 
       </td> 
       <td> 
       <g:textField name='amount' required id='amount'/> 
       </td> 
      </tr> 
      <tr> 
      <td> 
       <label for='billSubmittedDate'>Bill Submitted Date:</label> 
       </td> 
       <td> 
       <g:textField name='bill_submitted_dt' required id='bill_submitted_dt'/> 
      </td> 
      </tr> 
      <tr> 
      <td> 
       <label for='reImbursed'>Reimbursed:</label> 
       </td> 
       <td> 
       <g:textField name='reimbursed' required id='reimbursed'/> 
      </td> 
      </tr> 
      <tr> 
      <td> 
       <label for='submitBank'>Submitted to Bank:</label> 
       </td> 
       <td> 
       <g:textField name='presented_bank_fl' required id='presented_bank_fl'/> 
       </td> 
      </tr> 
      <tr> 
      <td> 
       <label for='dateOfSubmission'>Date of Bank Submission:</label> 
       </td> 
       <td> 
       <g:textField name='presented_bank_dt' required id='presented_bank_dt'/> 
       </td> 
      </tr> 
      <tr> 
      <td colspan="2" style="text-align:center;"> 
      <g:submitButton name="Submit" style="cursor:pointer"/> 
      </td> 
      </tr> 
     </table> 
    </g:formRemote> 
</body> 
</html> 

這裏是我的DataEntryController.groovy

package com.standout.utilityapplication 


import java.util.Map; 

import grails.plugin.springsecurity.annotation.Secured 
import groovy.sql.Sql 


@Secured(['ROLE_DATAENTRY']) 
class DataEntryController { 

    def dataSource 
    DataEntrySevice service 
    def index() { } 
    def dataentry(){ 
     println("Inside DataEntryController") 
    } 

    def saveAjax(){ 
     def result 
     try{ 
      service.saveDataentryDetails(params) 

      } 
      catch(Exception e){ 
       e.printStackTrace(); 
      } 

    } 

} 

這裏是我的DataEntryService.groovy

package com.standout.utilityapplication 

import grails.transaction.Transactional 
import groovy.sql.Sql 
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap 

@Transactional 
class DataEntrySevice { 

    def dataSource 
    def int saveDataentryDetails(Map params) { 
     def result 
     try{ 
      println("Inside saveDataentryAjax() method of dataentry") 


      Dataentry d = new Dataentry(); 
      d.amount = params.amount 
      d.bill_submitted_dt = params.bill_submitted_dt 
      d.employee_id = params.employee_id 
      d.number_of_persons = params.number_of_persons 
      d.presented_bank_dt = params.presented_bank_dt 
      d.presented_bank_fl = params.presented_bank_fl 
      d.receipt_dt = params.receipt_dt 
      d.reimbursed = params.reimbursed 
      d.restaurant_name = params.restaurant_name 
      d.team = params.team 
      d.save(failOnError:true); 

      } 
      catch(Exception e){ 
       e.printStackTrace(); 
      } 
      catch(Exception e){ 
       println(e) 
      } 
      return result 
    } 
} 

誰能幫助?

回答

0

在你的控制器中,你正在聲明一個DataEntryService類型的變量,但沒有實例化它。

DataEntryService service <-- service is null, hence your error 

DataEntryService service = new DataEntryService() 

並使控制器代碼保持原樣。

如果你想讓它自動的注入,那麼也許:

class DataEntryController { 
    def dataSource 
    def dataEntryService 

    def index() { } 

    def dataentry(){ 
     println("Inside DataEntryController") 
    } 

    def saveAjax(){ 
     def result 
     try { 
      dataEntryService.saveDataentryDetails(params) 

     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
相關問題