Spring autowired bean causes null pointer

45,239

Solution 1

When you create an object by new, autowire\inject don't work...

See this link and this link for some workaround.

Anyway if you would inject a logger i suggest you this my answer to another topic.

Solution 2

Just want to add my 2 cents.

I once encountered the same issue when I was not quite used to the life in the IoC world. The @Autowired field of one of my beans is null at runtime.

The root cause is, instead of using the auto-created bean maintained by the Spring IoC container (whose @Autowired field is indeed properly injected), I am newing my own instance of that bean and using it. Of course this one's @Autowired field is null because Spring has no chance to inject it.

Solution 3

If you are using AspectJ you can use @Configurable:

@Configurable
public class OurLogger {
  ..
}

See: Using AspectJ to dependency inject domain objects with Spring

Solution 4

Within your application context you have to do reference your Logger :

<context:component-scan base-package="com.platform"/>
<bean id="asyncLoggingService" class="com.platform.services.AsyncLoggingServiceImplementation" scope="prototype"/>
<bean id="ourLogger" class="com.platform.utils.OurLogger"/>

Then you've to inject it into your service :

@Service
public class AsyncAccountServiceImplementation implements AsyncAccountService
{

 private static final String GATEWAY_IP_BLOCK = "1";

 @Autowired
 private OurLogger logger;

}

Solution 5

Use spring framework Unit test instead of JUnit test to inject your spring bean.

May be that will help you.

Share:
45,239
robinjohnobrien
Author by

robinjohnobrien

Updated on January 08, 2020

Comments

  • robinjohnobrien
    robinjohnobrien over 4 years

    I have a logger class that makes use of a service. Each time a new logger is created, I expect to have access to the singleton scoped logging service.

    I autowire the logging service into the logger however, a null pointer exception is returned. I have tried a few solutions:

    1. manually defining the bean in the application context,
    2. Trying to get the logger to be spring managed but that just resulted in more issues.

    I am trying to get this to work in my junit tests, and I do specify the context file to make use of a different application context. However even if kept identical it does not resolve the issue.

    Please find code below:

    The following is an excerpt from the application context.

    <context:component-scan base-package="com.platform"/>
    <bean id="asyncLoggingService" class="com.platform.services.AsyncLoggingServiceImplementation" scope="prototype"/>
    

    The following is the Logger class.

    package com.platform.utils;
    
    
    import com.platform.services.AsyncLoggingService;
    import org.joda.time.DateTime;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    
    public class OurLogger
    {
    
      private static Logger logger;
    
      @Autowired
      private AsyncLoggingervice asyncLoggingService;
    
      public OurLogger(Class Clazz)
      {
        logger = LoggerFactory.getLogger(Clazz);
      }
    
    
      public void trace(TraceableObject object, String message)
      { 
        //Do nothing yet
      }
    

    }

    I then make use of the Logger in another service in order to log whats going on. (The reason I am writing another logger is to make use of an RabbitMQ server) In the service I instantiate a new instance of the Logger and then use it accordingly.

    @Service
    public class AsyncAccountServiceImplementation implements AsyncAccountService
    {
      private static final String GATEWAY_IP_BLOCK = "1";
    
      private static OurLogger logger = new      OurLogger(AsyncAccountServiceImplementation.class);
    
    ...
    }
    

    The null pointer occurs in the OurLogger when I try to call any method on the asyncLoggingService.

    I then am trying to test the AsyncAccountService using JUnit. I make sure I add the different application context but it still seems to result in the null pointer exception.

    If you need further information please let me know. I have seen ways to fix this but they don't seem to work so perhaps I have made a mistake somewhere or I am not understanding this all quite correctly.