Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullpointerException: cannot get property on null object

Working on IDEA and trying to launch the following code:

package com.myCompany.routing.spring

import com.dropbox.core.DbxRequestConfig
import grails.util.Holders
import spock.lang.Specification

class DropboxSpringConfigSpec extends Specification {
    def grailsApplication=Holders.grailsApplication

    def "It instantiates and configures the dropboxRequestConfig component"() {
        given:
        def ctx = grailsApplication.mainContext
        //do stuff...
    }
}

I get the following error:

java.lang.NullPointerException: Cannot get property 'mainContext' on null object

at com.myCompany.routing.spring.DropboxSpringConfigSpec.It instantiates and configures the dropboxRequestConfig component(DropboxSpringConfigSpec.groovy:20)

I've recently made a pull on my VCS, so the code should work.

When running the test as Grails test, I get the following error:

Error | 2015-03-04 13:32:00,989 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the application: Missing configuration in Config.groovy: connection.uri.

like image 768
Heschoon Avatar asked Oct 20 '22 16:10

Heschoon


1 Answers

Okay, it seems some configurations in Config.groovy were given the values of some environment variables:

elasticSearch {
    connection {
        uri = env.ES_URL
        username = env.ES_USER
        password = env.ES_PASSWORD
    }
    indexPrefix = 'test-'
}

Since I never created the corresponding environment variables, the GrailsContextLoader fails to find the corresponding value and the computation fail.

Initializing the required environmnent variables in my IDE and running the tests as Grails tests solved the problem.

like image 151
Heschoon Avatar answered Nov 03 '22 02:11

Heschoon