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

问题描述:

我正在使用grails版本:2.3.7来构建我的应用程序。当我运行我的应用程序,我得到的错误与空指针异常创建豆(完整的日志如下)错误context.GrailsContextLoader - 初始化应用程序时出错

| 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.s[email protected]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 

什么couldbe的原因是什么?如何调试它? 我会在回复后添加其他资源

这是我的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 
     } 
    } 
} 
+0

原因在stacktrace中解释。您可以修复DashboardController的第16行中的空指针异常(它在构造函数中)。为了进行调试,您可以在IDE中设置断点。 – cfrick 2015-03-02 14:30:43

的问题是与实例变量

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

DashboardController实例化时,试图执行上面的行,其中没有会话尚未存储(session.username是NULL)。

将这段代码移入方法内解决了问题。

这一行看起来NPE-可疑

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

要么使用

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

或其他记号:

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