I would like to create a helper class, but it just failed.. The error : java.lang.NullPointerException: null
When i do this without making a static class (with autowired) it works without problem. But it's a helper class, i think the static class is the better thing.
Thanks for help
Helper.java
public final class UrlHelper {
@Autowired
private static Environment bean;
public static String method(String projet) {
return "titi"+ bean.getProperty("property.name");
}
}
And in my service, i use it like this :
String list = getRequest.getHTTPRequest(UrlHelper.method(projet));
In Spring, the @Autowired
annotation allows you to resolve and inject beans into other beans. In your case, the UrlHelper
class is not a bean and therefore, your Environment
is not injected (stays null), hence the error. You have two options:
UrlHelper
a bean using @Component
, @Service
, etc. This will make your class non-static.UrlHelper
static, and pass the Environment
as parameter. This, I believe, is the more correct approach. The Environment can be injected in the class calling the static method.