带有静态 fields/methods 的反射和 NullPointerException?
Reflection and NullPointerException with static fields/methods?
我正在尝试使用反射来访问我不希望由对象继承的方法和变量。我收到这些 static 变量的空指针异常,我无法注意到我遗漏的任何内容。如果能帮助解决异常,我将不胜感激 :)
我是否遗漏了任何行,或者在需要时没有使用 Alien.class?
主要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();
}
}
}
外星人class:
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
returns Class<Alien>
的运行时间 class 即 Class.class
.然后,您在对 Class.class
的引用上调用 getDeclaredMethod
,查找名为 getNameList
的方法,这在 Class
class 和 NoSuchMethodException
被抛出,并且 getNameList
保持为空。
了解所有这些,解决方案是删除对 getClass
的调用,这样您就可以在对 Class<Alien>
的引用上调用 getDeclaredMethod
:
getNameList = Alien.class.getDeclaredMethod("getNameList");
我正在尝试使用反射来访问我不希望由对象继承的方法和变量。我收到这些 static 变量的空指针异常,我无法注意到我遗漏的任何内容。如果能帮助解决异常,我将不胜感激 :)
我是否遗漏了任何行,或者在需要时没有使用 Alien.class?
主要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();
}
}
}
外星人class:
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
returns Class<Alien>
的运行时间 class 即 Class.class
.然后,您在对 Class.class
的引用上调用 getDeclaredMethod
,查找名为 getNameList
的方法,这在 Class
class 和 NoSuchMethodException
被抛出,并且 getNameList
保持为空。
了解所有这些,解决方案是删除对 getClass
的调用,这样您就可以在对 Class<Alien>
的引用上调用 getDeclaredMethod
:
getNameList = Alien.class.getDeclaredMethod("getNameList");