Question

I have a Product class:

public class Product {

private ProductClass prodClass;

public ProductClass getProdClass() {
    return prodClass;
}

public void setProdClass(ProductClass prodClass) {
    this.prodClass = prodClass;
}
}

And another one ProductClass ...

public class ProductClass {

private String StbFlag;

public String getStbFlag() {
    return StbFlag;
}

public void setStbFlag(String stbFlag) {
    StbFlag = stbFlag;
}
}

When I try to get the Property using BeanUtils.getNestedProperty as shown below..

public class Test {

public static void main(String Args[]) {

    Product product = new Product();
    ProductClass proClass = new ProductClass();
    proClass.setStbFlag("abcd");
    product.setProdClass(proClass);

    try {
        String value = BeanUtils.getNestedProperty(product, "prodClass.StbFlag");
        System.out.println(value);
    } catch (Exception e) {
        e.printStackTrace();  
    }
}
}

Its throwing the following exception...

java.lang.NoSuchMethodException: Unknown property 'StbFlag' on class 'class ProductClass'
    at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1313)
    at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:762)
    at org.apache.commons.beanutils.BeanUtilsBean.getNestedProperty(BeanUtilsBean.java:715)
    at org.apache.commons.beanutils.BeanUtils.getNestedProperty(BeanUtils.java:354)
    at Test.main(Test.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)

What could be the reason? This is just a sample I used to find out the problem. I am actually mapping XML to Java Object and need to keep the name as StbFlag as per the xml tag.

It's working fine when I use STBflag or stbFlag as the variable name. Any workaround for this?

Was it helpful?

Solution

BeanUtils expects that your field names start with a lowercase letter, because it adheres to the JavaBean naming conventions.

In Section 8.8 of the JavaBeans spec:

Java programmers are accustomed to having normal identifiers start with lower case letters. Vigorous reviewer input has convinced us that we should follow this same conventional rule for property and event names.

Thus when we extract a property or event name from the middle of an existing Java name, we normally convert the first character to lower case. However to support the occasional use of all upper-case names, we check if the first two characters of the name are both upper case and if so leave it alone. So for example, “FooBah” becomes “fooBah” “Z” becomes “z” “URL” becomes “URL”

We provide a method Introspector.decapitalize which implements this conversion rule

That said, changing your code to this will fix it:

String value = BeanUtils.getNestedProperty(product, "prodClass.stbFlag");

If you are getting the string "StbFlag" from an XML file, I would recommend using the same decapitalize method that BeanUtils uses to convert it to the correct format.

Introspector.decapitalize("StbFlag")

which will return "stbFlag" as a result.

OTHER TIPS

public class ProductClass {

private String StbFlag;

public String getStbFlag() {
    return StbFlag;
}

public void setStbFlag(String stbFlag) {
    this.StbFlag = stbFlag;//error here write this get anwser
}
}

write this code anwser is coming

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top