null pointer exception with Grails rendering plugin
Asked Answered
O

2

9

I have some problems when using rendering plugin.It always returns me a null pointer exception.I saw severeal similar problems but I didn't find where I'm wrong.

  • Code of my template : /views/appRetail/_report.gsp

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-strict.dtd">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">
        <title>Welcome to Production !</title>
    </head>
    <html>
       <body>
        REPORT
       </body>
    </html>
    
  • Code of My controller :

    class AppRetailController {
    
      def pdfRenderingService
    
      def renderFormPDF() {
    
        def apps = App.findAll()
        new File("test.pdf").withOutputStream { outputStream ->
            pdfRenderingService.render(template: '/appRetail/report', model: [apps:apps], outputStream)
        }
      }
    }
    

Here is the stacktrace :

2015-04-17 10:31:54,552 [http-bio-8080-exec-4] ERROR   errors.GrailsExceptionResolver  - NullPointerException occurred when   processing request: [POST] /toolprod/appRetail/renderFormPDF
Stacktrace follows:
Message: null
Line | Method
->> 1281 | getPublicDeclaredMethods in java.beans.Introspector
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1141 | getTargetMethodInfo      in     ''
|    416 | getBeanInfo . . . . . .  in     ''
|    163 | getBeanInfo              in     ''
|     31 | init . . . . . . . . . . in     grails.plugin.rendering.document.RenderEnvironment
|     68 | with                     in     ''
|     60 | with . . . . . . . . . . in     ''
|     65 | generateXhtml            in     grails.plugin.rendering.document.XhtmlDocumentService
|     35 | createDocument . . . . . in     ''
|     36 | render                   in grails.plugin.rendering.RenderingService
|    348 | doCall . . . . . . . . . in     toolprod.AppRetailController$_renderFormPDF_closure1
|    347 | renderFormPDF            in toolprod.AppRetailController
|    198 | doFilter . . . . . . . . in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter                 in     grails.plugin.cache.web.filter.AbstractFilter
|     82 | doFilterInternal . . . . in com.linkedin.grails.profiler.ProfilerFilter
|   1145 | runWorker                in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . . . . . . . .  in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run                      in java.lang.Thread

Here is versions I used :

  • Plugin version : compile ":rendering:1.0.0"
  • Grails version : 2.5.0
Oversweet answered 17/4, 2015 at 9:3 Comment(0)
R
10

You need to add this dependency in your buildConfig file:

dependencies {
    runtime 'org.springframework:spring-test:4.1.6.RELEASE'
}
Ramayana answered 7/10, 2015 at 22:20 Comment(2)
Resolve error obtaining dependencies: Could not find artifact org.springframework:spring-test:zip:4.1.6.RELEASE in grailsCentral (repo.grails.org/grails/plugins) (Use --stacktrace to see the full trace)Frontiersman
Still necessary with rendering 2.0.3 in Grails 3.3.12. The reason is that GrailsWebMockUtil from grails-web-common uses MockHttpServletRequest from spring-test without having it as a runtime dependencyVengeance
H
0

From the stacktrace it looks like the withOutputStream closure is causing some confusion, but it worked for me in a test app. Try running grails clean and grails compile and re-running the app. If that doesn't fix it, delete the target directory and run clean and compile again.

If it's still a problem, there are a few different ways to generate a PDF. Instead of passing the OutputStream to iText to render the PDF, you can get the generated byte[] array and write it yourself, e.g.

def renderFormPDF() {
    def apps = App.findAll()
    ByteArrayOutputStream baos = pdfRenderingService.render(template: '/appRetail/report', model: [apps: apps])
    new File('test.pdf').withOutputStream { it.write baos.toByteArray() }
}

or

def renderFormPDF() {
    def apps = App.findAll()
    ByteArrayOutputStream baos = pdfRenderingService.render(template: '/appRetail/report', model: [apps: apps])
    new File('test.pdf') << baos.toByteArray()
}
Hindman answered 17/4, 2015 at 10:43 Comment(1)
thanks for you answer but I always has the same error.I also tried with renderPng : def file = new File("test.png") file.write("hello") renderPng(template: "/appRetail/report", model: [imageBytes: file.bytes])Oversweet

© 2022 - 2024 — McMap. All rights reserved.