java - NoSuchMethodException on trying to get Nested bean property -


i have product class:

public class product {  private productclass prodclass;  public productclass getprodclass() {     return prodclass; }  public void setprodclass(productclass prodclass) {     this.prodclass = prodclass; } } 

and 1 productclass ...

public class productclass {  private string stbflag;  public string getstbflag() {     return stbflag; }  public void setstbflag(string stbflag) {     stbflag = stbflag; } } 

when try property using beanutils.getnestedproperty 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 following exception...

java.lang.nosuchmethodexception: unknown property 'stbflag' on class 'class productclass'     @ org.apache.commons.beanutils.propertyutilsbean.getsimpleproperty(propertyutilsbean.java:1313)     @ org.apache.commons.beanutils.propertyutilsbean.getnestedproperty(propertyutilsbean.java:762)     @ org.apache.commons.beanutils.beanutilsbean.getnestedproperty(beanutilsbean.java:715)     @ org.apache.commons.beanutils.beanutils.getnestedproperty(beanutils.java:354)     @ test.main(test.java:15)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)     @ java.lang.reflect.method.invoke(method.java:601)     @ com.intellij.rt.execution.application.appmain.main(appmain.java:90) 

what reason? sample used find out problem. mapping xml java object , need keep name stbflag per xml tag.

it's working fine when use stbflag or stbflag variable name. workaround this?

beanutils expects field names start lowercase letter, because adheres javabean naming conventions.

in section 8.8 of javabeans spec:

java programmers accustomed having normal identifiers start lower case letters. vigorous reviewer input has convinced should follow same conventional rule property , event names.

thus when extract property or event name middle of existing java name, convert first character lower case. support occasional use of upper-case names, check if first 2 characters of name both upper case , if leave alone. example, “foobah” becomes “foobah” “z” becomes “z” “url” becomes “url”

we provide method introspector.decapitalize implements conversion rule

that said, changing code fix it:

string value = beanutils.getnestedproperty(product, "prodclass.stbflag"); 

if getting string "stbflag" xml file, recommend using same decapitalize method beanutils uses convert correct format.

introspector.decapitalize("stbflag") 

which return "stbflag" result.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -