错误context.GrailsContextLoader - 初始化应用程序时出错

时间:2015-03-02 14:10:40

标签: grails grails-controller

我使用grails版本:2.3.7来构建我的应用程序。当我运行我的应用程序时,我得到错误创建bean与空指针异常(完成日志如下)

| Running Grails application
2015-03-02 14:52:45,663 [localhost-startStop-1] INFO  context.ContextLoader  - Root WebApplicationContext: initializatio
n started
2015-03-02 14:52:45,769 [localhost-startStop-1] INFO  support.XmlWebApplicationContext  - Refreshing Root WebApplication
Context: startup date [Mon Mar 02 14:52:45 CET 2015]; root of context hierarchy
2015-03-02 14:52:45,830 [localhost-startStop-1] INFO  xml.XmlBeanDefinitionReader  - Loading XML bean definitions from S
ervletContext resource [/WEB-INF/applicationContext.xml]
2015-03-02 14:52:46,098 [localhost-startStop-1] INFO  support.DefaultListableBeanFactory  - Pre-instantiating singletons
 in org.springframework.beans.factory.support.DefaultListableBeanFactory@168484c: defining beans [grailsApplication,plug
inManager,grailsConfigurator,grailsResourceLoader,characterEncodingFilter,conversionService]; root of factory hierarchy
2015-03-02 14:52:50,516 [localhost-startStop-1] INFO  context.ContextLoader  - Root WebApplicationContext: initializatio
n completed in 4852 ms
2015-03-02 14:53:01,121 [localhost-startStop-1] INFO  quartz.SchedulerFactoryBean  - Shutting down Quartz Scheduler
Error |
2015-03-02 14:53:01,610 [localhost-startStop-1] ERROR context.GrailsContextLoader  - Error initializing the application:
 Error creating bean with name 'com.orga.odbc.DashboardController': Instantiation of bean failed; nested exception is or
g.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.orga.odbc.DashboardController]
: Constructor threw exception; nested exception is java.lang.NullPointerException
Message: Error creating bean with name 'com.orga.odbc.DashboardController': Instantiation of bean failed; nested excepti
on is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.orga.odbc.DashboardCon
troller]: Constructor threw exception; nested exception is java.lang.NullPointerException
    Line | Method
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run       in java.lang.Thread
Caused by BeanInstantiationException: Could not instantiate bean class [com.orga.odbc.DashboardController]: Constructor
threw exception; nested exception is java.lang.NullPointerException
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run       in java.lang.Thread
Caused by NullPointerException: null
->>   16 | <init>    in com.orga.odbc.DashboardController
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|    262 | run       in java.util.concurrent.FutureTask
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run . . . in java.lang.Thread
| Error Forked Grails VM exited with error

可能是什么原因?怎么调试呢? 我会在回复时添加其他资源

这是我的DashboardController.groovy。它会在登录时保存用户名会话LoginController

package com.orga.odbc

import grails.plugin.mail.MailService;

// import org.compass.core.engine.SearchEngineQueryParseException

import java.awt.TexturePaintContext.Int;

import com.sun.org.apache.xerces.internal.util.Status;

class DashboardController extends LoginController {

    //static String WILDCARD = "*"
    //def searchableService

    def usr = DbUser.findByLoginNameIlike(session.username) // Find DbUser reference to the current user
    ApplicationUtilityService appUtilService = new ApplicationUtilityService()
    MailService mailService
    def notifierService

    def index(Integer max) {
        log.info("***DASHBOARD***")

        int objCount = 0, dropObjCount = 0
        int schCount = 0, dropSchCount = 0
        int progressSch
        int progressObj

        params.max = Math.min(max ?: 25, 100)
        params.sort = params.sort?:"objects"
        params.order = params.order?:"desc"
        params.offset = params.offset?:0

        if(session.username.equals('admin')){ // If user is admin
            log.info('redirecting to admin index')
            render(view:"../index")
        }

        else if(usr){   // If user found
            def dbUsrSchSrvInstList = DbUserSchemaServer.findAllByUser(usr, [sort:params.sort, order:params.order, max:params.max, offset: params.offset]) // Schema, server info for current user
            def usrQuota

            appUtilService.updateUserStatus(usr) // Update the status of the current user

            if(usr.quota){
                usrQuota = [
                    name: usr.quota.identifier, // Quota identifier
                    schAllowed: usr.quota.maxSchemaCount,   // Max. number of schemas allowed
                    objAllowed: usr.quota.maxObjectCount    // Max. number of objects allowed
                ]

                // Count total number of objects used
                HashMap<String, BigInteger> schObjCount = appUtilService.getSchObjCount(usr)
                schCount = schObjCount.get("SCHEMAS")   // Schema count for the current user status no drop
                objCount = schObjCount.get("OBJECTS")   // Object count for the current user status no drop

                dropSchCount = schObjCount.get("DROP_SCHEMAS")  // Schemas with drop status
                dropObjCount = schObjCount.get("DROP_OBJECTS")  // Objects with drop status

                if(usrQuota.schAllowed != 0 && usrQuota.objAllowed != 0){   // protect from divided by zero
                    progressSch = (schCount / usrQuota.schAllowed) * 100        // Schema usage current user
                    progressObj = (objCount / usrQuota.objAllowed) * 100    // Object usage current user
                }
            }
            else{ // If user has no quota (May not happen for almost every user)
                usrQuota = [
                name: 'No quota assigned',  // Quota identifier
                schAllowed: 0,  // Max. number of schemas allowed
                objAllowed: 0   // Max. number of objects allowed
                ]

                schCount = 0
                progressSch = 0
                progressObj = 0
            }


            log.info('progress schema: '+ progressSch)
            log.info('progress objects: '+ progressObj)
            log.info('Total number of objects: '+ objCount+dropObjCount)

            //  Search procedure
            // def searchParamsList, searchParams, results_duplicates, results = []

            // If no query string, display all users list
            //if(!params.queryString){
                results = dbUsrSchSrvInstList
            //}
            // Else display results
            /* else{
                try {
                    String searchTerm = WILDCARD+params.queryString+WILDCARD    // take search term
                    searchParamsList = searchableService.search(dbUsrSchSrvInstList, searchTerm)    // search matching term
                    searchParams = searchParamsList.results // Collect results

                    // Find results match with the current user
                    results_duplicates = searchParams.findAll{
                        it.id in dbUsrSchSrvInstList*.id
                    }

                    // Find by server name. Match results and append to results list
                    results_duplicates = results_duplicates.plus(dbUsrSchSrvInstList.findAll {
                        it.server.id in searchParams.id
                    })

                        // remove duplicates in results
                    results_duplicates.id.unique().each {
                        results.add(DbUserSchemaServer.get(it))
                    }
                } catch (SearchEngineQueryParseException ex) {
                    ex.printStackTrace()
                }

            } */

            //return parameters
            [   dbUserInstance: usr,
                dbUsrSchSrvInstList: results,
                schCount: schCount,
                totalSchCount: schCount + dropSchCount,
                objCount:objCount,
                totalObjCount: objCount + dropObjCount,
                usrQuota:usrQuota,
                progressSch: progressSch,
                progressObj: progressObj
            ]   // return list of schemas related to the current user
        }
    }
}

2 个答案:

答案 0 :(得分:0)

这一行看起来像NPE一样可疑

if(session.username.equals('admin')){

使用

if(session.username?.equals('admin')){

或其他符号:

if('admin' == session.username){

答案 1 :(得分:0)

问题在于实例变量

def usr = DbUser.findByLoginNameIlike(session.username) // Find DbUser reference to the current user

DashboardController被实例化时,它会尝试执行尚未存储会话的上述行(session.username为NULL)。

在方法中移动这段代码解决了这个问题。

相关问题