1

In a spring boot application I am trying to load yaml property file and trying to use this class in @Autowire annotation in another class getting null pointer excepton

@Component
@Slf4j
@Data
public class ServiceGridConfiguration {

    public  AdapterConfig adapterConfig;



    public ServiceGridConfiguration() {
        Yaml yaml = new Yaml();
            String yamlFile ="ServiceGrid.yaml";
            try {

                AdapterConfig adapterConfig = yaml.loadAs( ServiceTypeResolver.class.getClassLoader()
                        .getResourceAsStream(yamlFile),AdapterConfig.class );
                System.out.println("adapterConfig" + adapterConfig.getSeedGroup());

                 this.adapterConfig = adapterConfig;

            } catch (Exception e) {
                log.error("Error in reading adapterConfig file "+yamlFile,e);
            }

    }

    }

In above constructor able to print in constructor while loading, but getting null pointer exception when hitting rest endpoint

public class ServiceGrid implements IhubStrategy {

    @Autowired
    ServiceGridConfiguration sfg;


    @Override
    public void processRequest(String message) throws Exception {
        System.out.println("ServiceGrid   "+message+"*******"+sfg.getAdapterConfig().getSeedEntity());
    }

}

@Component public class IhubStrategyFactory {

@Autowired
ServiceGridConfiguration sfg;

@Autowired
ServiceGrid sg;

public IhubStrategy getIhubStrategy(String serviceDefinition) {

    System.out.println("IhubStrategyFactory : "+sfg.getAdapterConfig().getSeedEntity());

// IhubStrategy strategy = null;

    if (serviceDefinition.equalsIgnoreCase("serviceGrid")) {

        return new ServiceGrid();
        /*try {
            sg.processRequest("serviceGrid");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/

    } else if (serviceDefinition.equalsIgnoreCase("TopologyResponse")) {

// strategy = new TopologyResponse(); }

    return null;
}

}

@Component
public class IhubStrategyFactory {

    @Autowired
    ServiceGridConfiguration sfg;

    @Autowired
    ServiceGrid sg;

    public IhubStrategy getIhubStrategy(String serviceDefinition) {

        System.out.println("IhubStrategyFactory : "+sfg.getAdapterConfig().getSeedEntity());

//      IhubStrategy strategy = null;

        if (serviceDefinition.equalsIgnoreCase("serviceGrid")) {

            return new ServiceGrid();
            /*try {
                sg.processRequest("serviceGrid");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }*/

        } else if (serviceDefinition.equalsIgnoreCase("TopologyResponse")) {
//          strategy = new TopologyResponse();
        }

        return null;
    }
}
  • 1
    Try Service instead of Component. – Jason Apr 11 '20 at 12:24
  • 2
    `ServiceGrid` should be annotated with service or component – GolamMazid Sajib Apr 11 '20 at 12:28
  • 2
    How you are injecting ServiceGrid class? annotation is missing on ServiceGrid – Niraj Sonawane Apr 11 '20 at 12:28
  • Does ServiceGrid have any Spring annotation (e.g. Component, Service), because if doesn't it can't be load in Spring Context ? – Lungu Daniel Apr 11 '20 at 12:28
  • Thanks all for replying, I annotated ServiceGrid with Service and component indivisiually but both didnt work !! – chethan bhounsley g Apr 11 '20 at 12:35
  • Could you show us the stack error you are getting ? – Lungu Daniel Apr 11 '20 at 12:38
  • @LunguDaniel, java.lang.NullPointerException at com.nokia.nsw.ihub.strategy.ServiceGrid.processRequest(ServiceGrid.java:17) at com.nokia.nsw.ihub.service.ServiceTypeResolver.invokeServiceClass(ServiceTypeResolver.java:28) at com.nokia.nsw.ihub.controller.BaseController.getBr(BaseController.java:26) – chethan bhounsley g Apr 11 '20 at 12:40
  • Even ServiceTypeResolver is annotated with Service annotation, (at)Service (at)Slf4j public class ServiceTypeResolver { public String invokeServiceClass(String serviceDefinition) { – chethan bhounsley g Apr 11 '20 at 12:42
  • @jason , while booting application I am getting value, 2020-04-11 18:13:11.907 INFO 121280 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3234 ms adapterConfig Cisco //// Here – chethan bhounsley g Apr 11 '20 at 12:44
  • Problem is I am creating object in factory class using new , public IhubStrategy getIhubStrategy(String serviceDefinition) { System.out.println("IhubStrategyFactory : "+sfg.getAdapterConfig().getSeedEntity()); // IhubStrategy strategy = null; if (serviceDefinition.equalsIgnoreCase("serviceGrid")) { return new ServiceGrid(); But how to return object to my resolver class ?? public void invokeServiceClass(String serviceDefinition) { IhubStrategy ihubStrategy = this.ihubFactory.getIhubStrategy(serviceDefinition); – chethan bhounsley g Apr 12 '20 at 15:30
  • Added code of my factory class @LunguDaniel, please suggest – chethan bhounsley g Apr 12 '20 at 15:39
  • I resolved this scenario using ApplicationContextAware. Thanks all for replying SpringBeanUtil.getBean(ServiceGrid.class); //////////////////////////////////////////// public class SpringBeanUtil implements ApplicationContextAware { private static ApplicationContext context; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } .......................... – chethan bhounsley g Apr 13 '20 at 05:16

1 Answers1

1

You have to annotate ServiceGrid with @Componenet or @Service, because at this moment it's not load in Spring Context and ServiceGridConfiguration can't be autowired.

@Service
public class ServiceGrid implements IhubStrategy {

    @Autowired
    ServiceGridConfiguration sfg;


    @Override
    public void processRequest(String message) throws Exception {
        System.out.println("ServiceGrid   "+message+"*******"+sfg.getAdapterConfig().getSeedEntity());
    }

}
Lungu Daniel
  • 816
  • 2
  • 8
  • 15