Someone may encounter such an error. A member variable is clearly defined in the class, but the system prompts an error while running: The property XX is not defined. Why?
As you know, a member variable has multiple access controllers. Generally, you should define the member variables as private ones, and then provide the set and get methods for the member variables, the set and get methods are used to operate member variables. In this way, only the member variables that can be accessed are attributes, for exampleCode:
Private string username;
Public void setusername (string username ){
This. Username = username;
}
Public String GetUserName (){
Return username;
}
At this time, we say that the current class has the username attribute. Note that it is case sensitive.
It is also very important that the attribute name is not determined by the member variable. Generally, the name of the Set Method and get method is set or get plus the name of the member variable. The first letter of the member variable must be capitalized. What is the attribute name? In fact, the attribute name is to remove set or get, and then change the first letter to lowercase. From the two naming rules, we can see that the attribute name is usually the same as the name of the member variable. However, if the set method and get method are not written according to the default rules, the attribute name is the same as the member variable name. See the following example:
Private string username;
Public void setusername (string username ){
This. Username = username;
}
Public String GetUserName (){
Return username;
}
In the above Code, only the method name is modified (uppercase N is changed to lowercase N). At this time, the attribute name has changed from username to username. If you want to access the get method by attribute name instead of directly accessing the get method (for example, Expression Language), or the framework automatically assigns a value to the attribute (for example, for webwork, you need to define attributes with the same name as the form element in action. For struts1, You need to define attributes with the same name as the form element in actionform). In this case, you must write username instead of username, otherwise, an error occurs.
If this error occurs, first check whether the set method or get method is defined, and then check whether the method name is correctly written.