For Programmers: Free Programming Magazines  


Home > Archive > Java Beans > August 2005 > Beanutils.getArrayProperty throws NoSuchMethodException for valid









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Beanutils.getArrayProperty throws NoSuchMethodException for valid
O.B.

2005-08-24, 3:56 am

I'm getting an odd problem when executing the Beanutils.getArrayProperty on
an attribute that I know exists. See the example below (TestClass.java and
TestBean.java). What am I doing wrong?

java.lang.NoSuchMethodException: Unknown property 'vTRate1'
at
org.apache.commons.beanutils.PropertyUtils.getSimpleProperty(PropertyUtils.java:1175)
at
org.apache.commons.beanutils.PropertyUtils.getNestedProperty(PropertyUtils.java:772)
at
org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:801)
at
org.apache.commons.beanutils.BeanUtils.getArrayProperty(BeanUtils.java:450)
at com.dyn.beantest.TestClass.print(TestClass.java:16)
at com.dyn.beantest.TestClass.main(TestClass.java:46)


TestClass.java:
package com.dyn.beantest;

import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;

public class TestClass {
public TestClass() {
}

public void print(TestBean theBean) {
try {
List exportFields = TestBean.getAttributeList();
for (int j = 0; j < exportFields.size(); j++) {
String[] val = BeanUtils.getArrayProperty(
theBean, (String) exportFields.get(j));
if (null != val) {
for (int k = 0; k < val.length; k++) {
String value = null != val[k] ? val[k] : "";
System.out.print(value);
if ((k + 1) < val.length) {
System.out.print(",");
}
}
}
if ((j + 1) < exportFields.size()) {
System.out.print(",");
}
}
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args) {
TestBean myBean = new TestBean();
TestClass thisTest = new TestClass();
thisTest.print(myBean);
}
}


TestBean.java:
package com.dyn.beantest;

import java.util.LinkedList;
import java.util.List;

public class TestBean {
private String testName;
private String vTRate1;
private String vTRate1Check;

public TestBean() {
}

public TestBean(String testName) {
this.testName = testName;
}

public static List getAttributeList() {
List attributeList = new LinkedList();
attributeList.add("testName");
attributeList.add("vTRate1");
attributeList.add("vTRate1Check");
return attributeList;
}

public String getTestName() {
return testName;
}

public void setTestName(String testName) {
this.testName = testName;
}

public String getVTRate1() {
return vTRate1;
}

public void setVTRate1(String rate1) {
vTRate1 = rate1;
}

public String getVTRate1Check() {
return vTRate1Check;
}

public void setVTRate1Check(String rate1Check) {
vTRate1Check = rate1Check;
}
}
Thomas Hawtin

2005-08-24, 7:56 am

O.B. wrote:
> I'm getting an odd problem when executing the Beanutils.getArrayProperty
> on an attribute that I know exists. See the example below
> (TestClass.java and TestBean.java). What am I doing wrong?
>
> [...]
>
> public static List getAttributeList() {
> List attributeList = new LinkedList();
> attributeList.add("testName");
> attributeList.add("vTRate1");
> attributeList.add("vTRate1Check");
> return attributeList;
> }
>
> [...]
>
> public String getVTRate1() {
> return vTRate1;
> }


Should that not be vtRate1 and vtRate1Check?

You should be able to get all the property names from
java.beans.Introspector.getBeanInfo.getPropertyDescriptors.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
O.B.

2005-08-24, 7:56 am

Thomas Hawtin wrote:
> O.B. wrote:
>
>
>
>
>
> Should that not be vtRate1 and vtRate1Check?
>
> You should be able to get all the property names from
> java.beans.Introspector.getBeanInfo.getPropertyDescriptors.
>
> Tom Hawtin


Very odd. Why does it not allow the 2nd letter of a class attribute to be
capitalized?
Thomas Hawtin

2005-08-24, 6:58 pm

O.B. wrote:
>
> Very odd. Why does it not allow the 2nd letter of a class attribute to
> be capitalized?


I forget the exact rules, but sequences of capital letters are assumed
to constitute one word (except the last character). Actually sequences
of capital letters right at the front of names may be treated differently.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com