java getClass().getField() 上的 NoSuchFieldException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10315614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NoSuchFieldException on getClass().getField()
提问by user1281029
java.lang.NoSuchFieldException: id
The below line is creating the exception.
下面的行正在创建异常。
String fieldValue =String.valueOf(studyplanCategory.getClass().getField(filterProperty).get(studyplanCategory));
studyplanCategory is a valid object and has got actual values. Beacuse of this exception the load method and the search function in the LazyLoading DataTable of my JSF webapp is not working.
studyplanCategory 是一个有效的对象,并具有实际值。由于此异常,我的 JSF Web 应用程序的 LazyLoading DataTable 中的加载方法和搜索功能无法正常工作。
回答by Perception
From the Javadocfor Class.getField(...)
:
来自Javadoc的Class.getField(...)
:
Returns a Field object that reflects the specified public member field of the class or interface represented by this Class object. The name parameter is a String specifying the simple name of the desired field. The field to be reflected is determined by the algorithm that follows. Let C be the class represented by this object:
If C declares a public field with the name specified, that is the field to be reflected. If no field was found in step 1 above, this algorithm is applied recursively to each direct superinterface of C. The direct superinterfaces are searched in the order they were declared. If no field was found in steps 1 and 2 above, and C has a superclass S, then this algorithm is invoked recursively upon S. If C has no superclass, then a NoSuchFieldException is thrown. See The Java Language Specification, sections 8.2 and 8.3.
返回一个 Field 对象,该对象反映了此 Class 对象表示的类或接口的指定公共成员字段。name 参数是一个字符串,指定所需字段的简单名称。要反射的场由以下算法确定。令 C 为该对象所代表的类:
如果 C 声明了一个具有指定名称的公共字段,那就是要反映的字段。如果在上面的步骤 1 中没有找到任何字段,则该算法将递归应用于 C 的每个直接超接口。直接超接口按照它们声明的顺序进行搜索。如果在上面的步骤 1 和 2 中没有找到字段,并且 C 有一个超类 S,那么这个算法会在 S 上递归调用。如果 C 没有超类,则抛出 NoSuchFieldException。请参阅 Java 语言规范,第 8.2 和 8.3 节。
If the field you are trying to retrieve via:
如果您尝试通过以下方式检索字段:
studyplanCategory.getClass().getField(filterProperty)
is private, then you will get a NoSuchFieldException
. For private fields, try this instead:
是私有的,那么你会得到一个NoSuchFieldException
. 对于私有字段,试试这个:
studyplanCategory.getClass().getDeclaredField(filterProperty)
And to get around potential illegal access exceptions when setting values via a field this way:
并以这种方式通过字段设置值时绕过潜在的非法访问异常:
Field field = studyplanCategory.getClass().getDeclaredField(filterProperty);
field.setAccessible(true);
field.get(studyplanCategory);
回答by Simon Dorociak
App fires up this exception because its doesn't see attribudes your want to give back. Method getField() return non-private attribudes so if your attribudes are private, method doesn't see them. You can check http://docs.oracle.com/javase/tutorial/reflect/member/fieldTrouble.html
应用程序触发此异常是因为它没有看到您想要回馈的属性。方法 getField() 返回非私有属性,因此如果您的属性是私有的,方法将看不到它们。您可以查看http://docs.oracle.com/javase/tutorial/reflect/member/fieldTrouble.html
So you can do that your attribudes will change on protected or public and then should work it right. But this way (its same like example on primefaces) simulate real database.
因此,您可以这样做,您的属性将在 protected 或 public 上更改,然后应该可以正常工作。但是这种方式(它与primefaces上的示例相同)模拟了真实的数据库。
public List<Car> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,String> filters) {
List<Car> data = new ArrayList<Car>();
//filter
for(Car car : datasource) {
boolean match = true;
for(Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
try {
String filterProperty = it.next();
String filterValue = filters.get(filterProperty);
String fieldValue = String.valueOf(car.getClass().getField(filterProperty).get(car));
...
So this list simulate real database only for example. If you want to use it. so you shoud do it on backing bean class and there do it. You open connection already with some filter or don't and then return data from database.
所以这个列表只是作为例子模拟真实的数据库。如果你想使用它。所以你应该在支持 bean 类上做它,然后在那里做。您已经使用某些过滤器打开连接或不打开连接,然后从数据库返回数据。
//EDIT: Man wrote that you should use getDeclaredField() but i did try this and it didn't work well, and throws up IlegalAccessException. When a pretype attribudes to protected, it works fine. I don't know why.
//编辑:Man 写道你应该使用 getDeclaredField() 但我确实尝试过这个,但效果不佳,并抛出了 IlegalAccessException。当预类型归属于受保护时,它工作正常。我不知道为什么。
回答by Murali Krishna Valluri
Best solutions for getClass().getField()
problem are:
getClass().getField()
问题的最佳解决方案是:
Use getDeclaredField() instead of getField()
使用getDeclaredField() 代替getField()
1)
1)
String propertyName = "test";<br/>
Class.forName(this.getClass().getName()).getDeclaredField(propertyName);
2)
2)
String propertyName = "name";<br/>
Replace **"HelloWorld"** with your class name<br/>
HelloWorld.class.getDeclaredField(propertyName)
回答by Muhamed Riyas M
I had faced the same problem. My issue was my variables are not public. Make sure your class variables are public
我遇到了同样的问题。我的问题是我的变量不是public。确保您的类变量是公开的
private Object getVariableValue(Object clazz, String variableName) {
try {
Field field = clazz.getClass().getField(variableName);
return field.get(clazz);
} catch (NoSuchFieldException e) {
return "";
} catch (IllegalAccessException e) {
return "";
}
}