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

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
kanni
  • 73
  • 1
  • 11

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
  • 12,410
  • 3
  • 41
  • 67
0

I ended up doing this:

@Value("#{'${prop.myVariable}'.split(',')}")
private List<String> allowedCharacteristics;
Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
kanni
  • 73
  • 1
  • 11
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
  • 136
  • 2
  • 9