mercredi 5 octobre 2016

Java Reflection: Code runs fine in Debugger but not in "normal" running mode

I'm trying to call a static Method from an Array of Methods. This works just fine in the debugger but not in normal running mode.. Why is this?

More description in code comments below..

My main method looks like this:

    //Get all methods of class
    Method[] backaudomadMethoden = Backautomat.class.getMethods();
    //Get first Method of class -> I know this one is static -> see in source "Backautomat"
    Method backMethod =  backaudomadMethoden[0];
    //Printing out Method Name: In Debugger this returns the static method name: getBezeichnung(),
    //In "normal" running mode (Run -> Run as -> Java Application) it prints out the second method: backautomat_starten()
    System.out.println(backMethod.getName());
    //Invocation is successfull in debugger
    //Invocation throws nullpointer exception running in "normal" mode
    String.valueOf(backMethod.invoke(null)); //nullpointer at this line

Here my Backautomat class with the static Method: public class Backautomat {

private String aktuellBackendeBrotsorte = "Butterbrot";

//Test für Statische Methoden: Brauche ich dazu auch eine Instanz für Invoke? 
public static String getBezeichnung(){
    return "Bezeichnung: Bester-Backautomat-Ever";
}

//Test für Methoden ohne Parameterliste
public boolean backautomat_starten(){
    return true;
}

}





Aucun commentaire:

Enregistrer un commentaire