[英]Using java reflection to access field of static nested class's fields throws NullPointerException
[英]Reflection and NullPointerException with static fields/methods?
我正在嘗試使用反射來訪問我不想被對象繼承的方法和變量。 我收到這些靜態變量的空指針異常,我沒有注意到我遺漏的任何內容。 我將不勝感激幫助解決異常:)
我是否遺漏了任何行,或者在需要時不使用 Alien.class?
主要類:
public class Main {
public static void main(String[] args)
{
System.out.println("Starting process of alien creation...");
Alien alien_1 = new Alien("Bob", 5, (float) 45.3, AlienType.REACTIVE);
Alien alien_2 = new Alien("Lilly", 7, (float) 49.8, AlienType.FRIENDLY);
System.out.println("\n" + alien_1.getName());
alien_1.setName("Carl");
System.out.println(alien_1.getName());
Method getNameList = null;
Field nameList = null;
try {
getNameList = Alien.class.getClass().getDeclaredMethod("getNameList");
} catch (NoSuchMethodException e) {
System.out.println("ERROR - Method \"getNameList\" does not exist!");
} catch (SecurityException e) {
System.out.println("ERROR - Method \"getNameList\" cannot be used!");
}
try {
nameList = Alien.class.getClass().getDeclaredField("alienNames");
} catch (NoSuchFieldException e) {
System.out.println("ERROR - Field \"nameList\" does not exist!");
} catch (SecurityException e) {
System.out.println("ERROR - Field \"nameList\" cannot be used!");
}
getNameList.setAccessible(true);
nameList.setAccessible(true);
try {
ArrayList<String> returnValue = (ArrayList<String>) getNameList.invoke(new Alien(), nameList);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
外星人類:
public class Alien
{
public void getAndPrintType(AlienType type)
{
switch (type)
{
case FRIENDLY : System.out.print(", type - FRIENDLY.\n"); break;
case SCARY : System.out.print(", type - SCARY.\n"); break;
case REACTIVE : System.out.print(", type - REACTIVE.\n"); break;
}
}
String name;
int age;
float weight;
AlienType type;
int totalAliens;
public static ArrayList<String> alienNames = new ArrayList<String>();
ArrayList<Integer> alienAges = new ArrayList<Integer>();
ArrayList<Float> alienWeights = new ArrayList<Float>();
ArrayList<AlienType> alienTypes = new ArrayList<AlienType>();
float totalWeight;
public Alien(String name, int age, float weight, AlienType type)
{
System.out.print("You have created an alien of attributes: ");
System.out.print("name - " + name );
System.out.print(", age - " + String.valueOf(age));
System.out.print(", weight - " + String.valueOf(weight) + " pounds");
getAndPrintType(type);
this.name = name;
this.age = age;
this.weight = weight;
this.type = type;
// Global impact
totalAliens++;
alienNames.add(name);
alienAges.add(age);
alienWeights.add(weight);
alienTypes.add(type);
}
public Alien() {}
private static String getNameList()
{
String returnValue = "";
for (String nameLocal : alienNames)
{
if (!(alienNames.get((alienNames.size() - 1)).equals(nameLocal)))
{
returnValue += nameLocal + ", ";
}
else
{
returnValue += nameLocal;
}
}
return returnValue;
}
}
問題似乎出在這一行:
getNameList = Alien.class.getClass().getDeclaredMethod("getNameList");
Alien.class
成為對Class<Alien>
的引用,因此getClass
返回Class<Alien>
的運行時類,即Class.class
。 然后在Class.class
的引用上調用getDeclaredMethod
,查找名為getNameList
的方法, getNameList
, Class
類中不存在該方法,並且NoSuchMethodException
,而getNameList
保持為空。
了解所有這些后,解決方案是刪除對getClass
的調用,以便您在對Class<Alien>
的引用上調用getDeclaredMethod
:
getNameList = Alien.class.getDeclaredMethod("getNameList");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.