我试图使用反射来访问我不希望被对象继承的方法和变量。对于这些静态变量,我收到一个空指针异常,并且我无法注意到我错过的任何内容。如果您能帮忙解决这个异常,我将不胜感激:)
我是否缺少任何行,或者在需要时不使用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
的方法,这在Class
类中不存在,并且抛出NoSuchMethodException
,并且getNameList
保持为空。
知道了所有这些,解决方案是删除对getClass
的调用,这样你就可以在引用Class<Alien>
时调用getDeclaredMethod
:
getNameList = Alien.class.getDeclaredMethod("getNameList");