1

I cannot get the property with underscore (ex: User_Name) from a class using BeanUtils.getProperty(bean, property), it always throw error:

"Unknown property 'User_Name' on class 'User'".

But when I debug the bean, it has User_Name property.

BeanUtils.getProperty(bean, propertyName);

The User class is

public class User {
    private String ID;
    private String User_Name;

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getUser_Name() {
        return this.User_Name;
    }

    public void setUser_Name(String user_Name) {
        this.User_Name = user_Name;
    }
}
samabcde
  • 3,622
  • 1
  • 18
  • 28
rathaom
  • 23
  • 2
  • Please also post your class containing **User_Name**. – samabcde Jul 17 '19 at 04:43
  • When trying to resolve programming errors, quite often we need to start with locating the cause of the error. In most cases the [stack trace](https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html#printStackTrace--) is essential in locating the cause. Please post the entire error message and stack trace you are getting. – Abra Jul 17 '19 at 05:06
  • If it is `BeanUtils` shouldnt it call `getUser_Name()` getter? Maybe there is not such getter. IDK – Antoniossss Jul 17 '19 at 05:35
  • Hi @samabcde, here the class for bean `public class User { private String ID; private String User_Name; public void setID(String ID) { this.ID = ID; } public String getUser_Name() { return this.User_Name; } public void setUser_Name(String user_Name) { this.User_Name = user_Name; }` – rathaom Jul 17 '19 at 09:29
  • HI @Abra The error is only shown Unknown property 'User_Name' on class 'path.to.class'. – rathaom Jul 17 '19 at 09:34
  • Hi @Antoniossss It works with another property, like ID. but when I use with property User_Name, it doesn't work. – rathaom Jul 17 '19 at 09:40
  • I assume you are referring to this [getProperty](https://commons.apache.org/proper/commons-beanutils/apidocs/org/apache/commons/beanutils/BeanUtils.html#getProperty-java.lang.Object-java.lang.String-) method in class `BeanUtils`. As you can see from the _javadoc_, that method throws three different kinds of `Exception`. So in your code, put the call to method `getProperty()` in a `try-catch` and in the `catch` block call method `printStackTrace()`. Then you **will** get a stack trace. Then you can [edit] your question and post that stack trace. – Abra Jul 17 '19 at 17:01
  • The problem is not with the underscore, the problem is with the property name `User_Name`. Simply changing the name to `user_Name` should work for you. – Abra Jul 17 '19 at 17:06
  • Alternatively, you can create a `BeanInfo` class. – Abra Jul 17 '19 at 17:16

1 Answers1

2

It is a matter of naming convention. You can refer to Where is the JavaBean property naming convention defined? for reference. From section 8.8 of JavaBeans API specification

... 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*case 1. However to support the occasional use of all upper-case names, we check if the first two characters*case 2 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

Hence for your given class, the property deduced from getUser_Name() and setUser_Name() is "user_Name" instead of "User_Name" according to *case1. And calling getProperty(bean, "ID") is working according to *case 2.

To solve the problem, please update the naming according to the Java naming convention, we should start with lower case for property and method, and use camelCase instead of snake_case to separate word. Keep in mind that following convention is really important in programming. The following is the updated class as an example.

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;

public class User {
    private String ID;
    private String userName;

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public static void main(String[] args)
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        User bean = new User();
        bean.setUserName("name");
        System.out.println(BeanUtils.getProperty(bean, "userName"));
    }
}
Community
  • 1
  • 1
samabcde
  • 3,622
  • 1
  • 18
  • 28