简体   繁体   中英

Not able to read BigDecimal property in java getter method

I am having a property discount=30 in properties file and declaring it as below in jave file :

private BigDecimal discount;
public void setDiscount(BigDecimal discount) {
    this.discount = discount;
}
public BigDecimal getDiscount() {
    return discount;
}

getDiscount() is returning NULL when i try to get its value . But I need it as BigDecimal only . I dont have to declare it as String or double and type cast it though that will work. Using String or double is working fine except for Big Decimal. Please help me to fix this or what is the reason BigDecimal is not working in this case ?

It think it will work fine, but if it returns null you didnt set it before calling the getter. If you are not able to ensure that the value is set before calling the getter, you can instantiat it in the class. for example with 0.

private BigDecimal discount = new BigDecimal(0);

and if you call the setter you have to do convert the value you would pass to an long or double and then use BigDecimal.valueOf() .

In Properties files its always returned as String.

While getting the data from properties file convert it into BigDecimal then use it.

SelectClient c= new SelectClient();
BigDecimal b = new BigDecimal(prop.getProperty("data"));
c.setDiscount(b);
System.out.println(c.getDiscount());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM