Creating Resource type constructor with @Value threw BeanCreationException; nested NullPointerException

Viewed 97

I am writing a springboot project, where I am picking a .properties file from application.properties

application.properties

sample.prop.file=com.example.handler/signature.properties

and have created a Component to pass this file as InputStream and initialise the Bean

Handler.java

@Component
public class Handler extends DefaultHandler {

private Resource properties;

public Handler(@Value("${sample.prop.file}") Resource properties) throws IOException {
    this.properties = properties;
    this.props = new Properties();
    this.props.load(this.properties.getInputStream());
  }
}

In my service I create an instance of Handler.java and add it as BindingProvider

Service.java

@Service
public class Service {

@Autowired
private Handler handler;

public void doSomething() {
    //some logic
    someMethod(handler);
}

After following some similar queries I am trying constructor injection and using @Autowired in my Service to make sure my @Component is initialised before it is called. But still upon running the project I am getting an error below.

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Handler' defined in file [/Users/abc/APP/testService/target/classes/com/example/handler/Handler.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.handler.Handler]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1320)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1287)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:636)
... 32 common frames omitted

Any inputs/comments are appriciated. Thanks!

2 Answers

If the file in classpath. put classpath: as follows.

@Value("classpath:${sample.prop.file}")

You should know that Resource is annotation and you can't create an instance of it.

So you may change properties from Resource to String because @Value("${sample.prop.file}") returns String. Your class may become like so:

@Component
public class Handler extends DefaultHandler {

    private Properties props;

    public Handler(@Value("${sample.prop.file}") String properties) throws IOException 
    {
        this.props = new Properties();
        String p =  Paths.get(properties).toAbsolutePath().toString();
        InputStream in = new FileInputStream(p);
        this.props.load(in);
    }
}

But make sure that the new file is accessable, i.e. in some absolute path!

Related