............错误| java.lang.NullPointerException:无法在null对象

时间:2016-08-26 08:09:47

标签: grails

当我尝试保存表单详细信息时出现以下错误。 (当我点击我的dataentry.gsp文件中的提交按钮时) 我认为在我调用服务的控制器上发生了错误。

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

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

在控制器中,您声明了一个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();
        }
    }
}