Search code examples
grailsgrails-controller

ERROR context.GrailsContextLoader - Error initializing the application


I am using grails version: 2.3.7 to build my application. When I run my application, I get the error creating bean with null pointer exception (complete log as follows)

| 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

What couldbe the reason? How to debug it? I'll add the other resources upon response

Here is my DashboardController.groovy. It extends LoginController where I save username session upon login.

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

Solution

  • The problem was with the instance variable

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

    When DashboardController is instantiated, it tries to execute the above line where no session is stored yet (session.username is NULL).

    Moving this piece of code inside the methods solved the problem.