:EDIT例外は次のようになります
public class Stackoverflowquestion {
public static 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;
}
}
public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//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 exception running in "normal" mode
System.out.println(String.valueOf(backMethod.invoke(null)));
}
https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Method.html#invoke(java.lang.Object,%20java.lang.Object...)
これは、 Uは、あなたがオブジェクトとしてnullを渡しているので、あなたがnullのオブジェクトのメソッドを呼び出そうとしているここで定義されたオブジェクトに渡された
String.valueOf(backMethod.invoke(null));
にそのメソッドを呼び出します。明らかに
Object x = null;
x.toString();
とx.toString(のようなものをやっへの同様の
)NPE
出典
2016-10-05 13:02:59
Ash
例外はありますか?別のビットをコピーして別のファイルに貼り付けなくても、私たち自身で複製できるように[mcve]を提供できますか? (新しいファイルを1つ作成し、コピー、貼り付け、コンパイルして実行し、エラーを表示できるはずです) –
['Class.getMethods()'](https://docs.oracle.com/javase/8/) docs/api/java/lang/Class.html#getMethods--)は保証された順序でメソッドを提供しません。デバッガ*で実行すると、注文に影響を与えることができます。実際には、* anything *はその順序に影響を与えることができますが、デバッガの場合もそうです... – Holger
getMethods()のドキュメントから: "返された配列の要素はソートされず、特定の順序ではありません" – Zircon