0

I have properties file with below list of values

prop.myVariable=v1,v2,v3

i tried to read them using spring boot as below:

@Value("#{'${prop.myVariable}'.split(',')}")
public static List<String> allowList;

When i was trying to execute it, it's not able to read and getting java.lang.NullPointerException

YCF_L
  • 51,266
  • 13
  • 85
  • 129
kanni
  • 73
  • 1
  • 10

3 Answers3

1

Static members are initialized before loading the properties. To workaround this issue, use setter injection:

public static List<String> allowList;

@Value("#{'${prop.myVariable}'.split(',')}")
public void setAllowList(List<String> list) {
    allowList = list;
}
Aniket Sahrawat
  • 11,710
  • 3
  • 36
  • 63
0

I ended up doing this:

@Value("#{'${prop.myVariable}'.split(',')}")
private List<String> allowedCharacteristics;
Aniket Sahrawat
  • 11,710
  • 3
  • 36
  • 63
kanni
  • 73
  • 1
  • 10
0

As I can see you are using the following code, why would you even want to save the properties to a "list"

List<String> allowList;
@Value("#{'${prop.myVariable}'.split(',')}") 
public List<String> setAllowList(List<String> list) { 
  this.list= list;
 } 
String Chars = myProperties.getConfigValue("prop.myVariable"); 
List<String> allowedCharacteristics = setCharacteristics(Chars); 

You have to store the properties to "allowlist", use the following code below

@Value("#{'${prop.myVariable}'.split(',')}")
public void setAllowList(List<String> list) {
    allowList = list;
}

please follow this thread -> How to access a value defined in the application.properties file in Spring Boot if you have to specifically use "getConfigValue()".

bhanu prakash
  • 106
  • 2
  • 9