iswg
iswg

Reputation: 57

Having trouble retrieving integer from .properties

I keep getting the java.lang.NumberFormatException: null error when I am trying to use an integer that I set in application.properties.

This is my application.properties

my.prop.for.func=25

This is how I retrieved the value from the .properties file and converted it to an integer to use in a function later on.

@Value("${my.prop.for.func}")
private String myPropForFuncStr;
private int myPropForFunc = Integer.parseInt(myPropForFuncStr);

I tried other ways of converting the property to an integer such as @Value("#{new int ('${my.prop.for.func}')}") but they gave other errors and this seemed like the easiest way.

I am probably getting the NumberFormatException: Null error because myPropForFuncStr is null. I have always retrieved values from .properties file like this so why is it messing up right now?. If that isn't the problem, what else could give the NumberFormatException: Null error?

Upvotes: 1

Views: 2985

Answers (3)

Srikanth Anusuri
Srikanth Anusuri

Reputation: 1409

Firstly Spring framework already handles the autoboxing for you. Which means that you can directly add the annotation to the int variable.

@Value("${my.prop.for.func}")
private Integer myPropForFunc; // Note that this needs to be an object and not a primitive data type.

Now, coming to your issue, you may want to check if your class has been annotated with the @PropertySource annotation. If you are using Spring boot to initialize your application, then it should have automatically read your application.properties file and injected these properties to the fields appropriately. But if you are not, then you will need to annotate your class with @PropertySource annotation. Even if you were using Spring boot, your setup would not have worked since the @Value will be evaluated after the bean has been successfully initialized, but you are initializing the value of the myPropForFunc at the field itself.

Here is an example which shows the usage of the annotation. https://www.mkyong.com/spring/spring-propertysources-example/

Upvotes: 4

myPropForFuncStr is not initiated at the time you try to convert it to int. I would suggest to add @Value directly on myPropForFunc

@Value("${my.prop.for.func}")
private int myPropForFunc = Integer.parseInt(myPropForFuncStr);

Upvotes: 0

Noixes
Noixes

Reputation: 1178

Spring initializes classes with their annotated attributes after instantiation. That means, the call "Integer.parseInt" will be called before spring can load the properties, because the class already has been initialized.

You have several options to solve this:

  1. Write an void init method with the @PostConstruct annotation. In this method, all spring related informations are already loaded.

  2. Write an getter, that always parses to integer on call in runtime

  3. I think spring can also handle:

    @Value("${my.prop.for.func}") private Integer myPropForFuncInt;

Upvotes: 1

Related Questions