Search code examples
grailsmodel-view-controllernullpointerexceptiongsp

Grails NPE on root context?


My Grails (2.4.2) project's basic structure:

my-app/
    grails-app/
        views/
            web/
                index.gsp
                signin.gsp
            admin/
            app/
        controllers/
            myapp/
                WebController.groovy
    <rest of project is a normal Grails app>

My application.properties:

#Grails Metadata file
#Thu Nov 06 14:21:10 EST 2014
app.grails.version=2.4.2
app.name=my-app
app.context=/
app.version=0.1

My UrlMappings:

class UrlMappings {
    static mappings = {
        "/$controller/$action?/$id?(.$format)?"{
            constraints {
                // apply constraints here
            }
        }

        "/"(controller: "web")
    }
}

My WebController:

package myapp

class WebController {
    def index() {
        render(view: "web/index")
    }

    def signin() {
        render(view: "web/signin")
    }
}

When I run-app:

|Loading Grails 2.4.2
|Configuring classpath
.
|Environment set to development
.................................
|Packaging Grails application
....................................
|Running Grails application
|Server running. Browse to http://localhost:8080/

When I click that link (http://localhost:8080/):

....[ERROR] 2014-11-19 10:22:44,029 org.codehaus.groovy.grails.web.errors.GrailsExceptionResolver - NullPointerException occurred when processing request: [GET] /
Stacktrace follows:
java.lang.NullPointerException
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:198)
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

My intentions are as follows:

  • All web pages controlled by the WebController
  • All web page views stored under grails-app/views/web)
  • URL mapping such that http://localhost:8080/ takes you to grails-app/views/web/index.gsp
  • URL mapping such that http://localhost:8080/signin takes you to grails-app/views/web/signin.gsp

What's going on here?


Solution

  • One problem you have is with the following:

    // ...
    
    render(view: "web/index")
    
    // ...
    
    render(view: "web/signin")
    
    // ...
    

    The problem with those is you are specifying a relative path. It will be relative to grails-app/views/web/ since you are in the Web controller. You could make them absolute like this...

    // ...
    
    render(view: "/web/index")
    
    // ...
    
    render(view: "/web/signin")
    
    // ...
    

    But the more common thing to do is allow them to be relative like this...

    // ...
    
    render(view: "index")
    
    // ...
    
    render(view: "signin")
    
    // ...