Getting NullPointerException when tried to read @Autowired configuration object [duplicate]
Getting NullPointerException when tried to read @Autowired configuration object [duplicate]
This question already has an answer here:
I am trying to read some properties from application.properties. I have created a configuration class with @Component and @ConfigurationProperties annotations.
application.properties
@Component
@ConfigurationProperties
When I am trying to access the configuration from a controller class, its working fine. But when I am trying to access the configuration from one component class, its throwing a null pointer exception.
Following are the application.properties and classes.
application.properties
application.properties
elasticsearch.ip=localhost
InputManagementController.java
@RestController
public class InputManagementController {
@Autowired
private Configuration configuration;
@GetMapping("/crawler/start")
public String start(){
try{
System.out.println(configuration.getIp()); ----> getting value localhost
ElasticsearchInterface es=new ElasticsearchInterface();
es.getInputs();
}catch (Exception e){
e.printStackTrace();
}
return "started";
}
}
ElasticsearchInterface.java
@Component
public class ElasticsearchInterface {
@Autowired
private Configuration configuration;
public List<Map<String, Object>> getInputs() {
System.out.println(configuration.getIp()); ---> getting java.lang.NullPointerException here
return null;
}
}
Configuration.java
@Component
@ConfigurationProperties("elasticsearch")
public class Configuration {
private String ip;
private int port;
private String cluster;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
}
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
@Deadpool still getting the same error
– TweetMan
Jul 1 at 16:12
it's working for me, are these class in same package? or different? and also check this dependency <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
– Deadpool
Jul 1 at 16:58
ElasticsearchInterface es=new ElasticsearchInterface(); doesn't work, you have to inject ElasticsearchInterface as well, so that it's managed by Spring.– dunni
Jul 1 at 19:40
ElasticsearchInterface es=new ElasticsearchInterface();
You are doing it all wrong, you need to use @Configuration annotation so that your class is going to be created in the Spring Context automatically. Because that you don't mark your Configuration class with @ Configuration annotation, it is not created as a bean and you get null pointer exception. Also you should use @ Value annotation so that you define which properties you are going to read. I've explained how to do this correctly via my answer below.
– Levent Divilioglu
Jul 1 at 22:36
1 Answer
1
On of the default location of application.properties file is src/main/resources. Create this folder and also this file. Assuming your scenario, put the following properties inside application.properties file;
myconfig.ip=192.168.166.42
myconfig.port=8090
In order to read the properties file, you need a class with @Configuration annotation. You need appropriate fields with @Value annotation for all the properties that you need to read. In addition, also you need the getter methods for these fields marked with @Value annotation. Please note that naming this class as "Configuration" is a very bad choice because there is also an annotation with the same name, thus I name it as "AppConfig"
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Value("${myconfig.ip}")
private String ip;
@Value("${myconfig.port}")
private int port;
public String getIp() {
return ip;
}
public int getPort() {
return port;
}
}
Then you create a fields of this AppConfig class which is written above, and mark it with @Autowired annotation. Then you can read the properties files easily;
@RestController
@RequestMapping("/")
public class InputManagementController {
@Autowired
private AppConfig config;
@RequestMapping("/test")
public String test() {
return String.format(
"Configuration Parameters: Port: %s, Ip: %s",
config.getPort(),
config.getIp()
);
}
}

@RestController
public class InputManagementController {
@Autowired
private AppConfig configuration;
@Autowired
private ElasticSearchInterface elasticSearchInterface;
@GetMapping("/crawler/start")
public String start() {
try {
System.out.println(configuration.getIp());
es.getInputs();
} catch (Exception e) {
e.printStackTrace();
}
return "started";
}
}
@Component
public class ElasticSearchInterface {
@Autowired
private AppConfig configuration;
public List<Map<String, Object>> getInputs() {
System.out.println(configuration.getIp());
return null;
}
}
@Configuration
public class AppConfig {
@Value("${myconfig.ip}")
private String ip;
@Value("${myconfig.port}")
private int port;
public String getIp() {
return ip;
}
public int getPort() {
return port;
}
}
Thanks for the detailed writeup. I tried AppConfig class But Still I am getting the same error. I am able to read the properties within the controller class. But i am still not able to read the properties in my service class ElasticsearchInterface. Still its throwing a null pointer exception.
– TweetMan
Jul 2 at 9:12
The problem there is, you are using a new operator instead of using autowiring. Create an interface, implement it, and use the interface as a field in your class then autowire it. The reason you are getting null pointer exception is your class instance is not on Spring Context. I've updated my answer, added a second section.
– Levent Divilioglu
Jul 2 at 9:36
Thank you. Its working now :)
– TweetMan
Jul 2 at 11:13
instead of Component annotation use Configuration annotation on top of Configuration.java class @user
– Deadpool
Jul 1 at 16:07