mercredi 9 septembre 2015

Reflection and NullPointerException with static fields/methods?

I am trying to use reflection to access a method and variable that I do not want inherited by an object. I receive a null pointer exception with these static variables, and I cannot notice anything I have missed. I would appreciate help in resolving the exception :) Am I missing any lines, or not using Alien.class when I need to?

Main 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();
    }
}

}

Alien 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;
}
}





Aucun commentaire:

Enregistrer un commentaire