• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Grails Services and command objects nullpointerexception

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have a controller which creates a command object (which is placed under src/groovy). This command object has a non-static method in which I want to use a service which is injected in the command. I face a nullpointerexception here.

I feel this should work as this is pretty straightforward. I am using grails 2.2.0

Example code
src/groovy




I tried th e following but these did not work.
1. to place the service in resources.groovy
2. Autowire the someService explicitly
3. tried to use grailsApplication.mainContext // in this case the grailsApplication was null
4. tried to use @Configurable on the TTTCommand and @Autowired on the service

Any pointers would be of great help.

 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please UseCodeTags (← click) when you post code. I've done it for you this time.

I think we will need the full stack trace of the error and the code around where the error occurs.
 
Oliver Ferns
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its at line number 10 which throws nullpointerexception
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would @Autowire someService, run it, and post the stacktrace. The code as it stands will always throw a NPE.
 
Oliver Ferns
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did try @Autowire over the service, but it didn't help. (the domain object was created using new operator)

However using the following construct

def someService = ApplicationHolder.application.mainContext.someService

helped to overcome the problem.

I need to understand why it will throw NPE thought spring would autowire the service?

My guess, is objects created using new operator become non managed spring objects and hence spring does not know how to autowire it. Am I correct to interpret it this way.


 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're autowiring an object, you should not instantiate it with the new operator -- you are correct.
reply
    Bookmark Topic Watch Topic
  • New Topic