Hey all I am having an issue fixing the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxxxxxxxxxx/com.xxxxxxxxxxx.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.reflect.Method.getName()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.reflect.Method.getName()' on a null object reference
at com.xxxxxxxxxxxx.model.GlobalCls.logData(GlobalCls.java:301)
at com.xxxxxxxxxxxx.MainActivity.onCreate(MainActivity.java:248)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
My code I'm calling:
@Override
protected void onCreate(Bundle savedInstanceState) {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
GlobalCls.hideSystemUI(getWindow());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
... more code here...
if (GlobalCls.isDebugging) { GlobalCls.logData(true, new Object(){}.getClass(), null); }
... more code here...
}
And the class I am calling GlobalCls:
public static void logData(Boolean isErr, Class _c, List<String> txt, String... _Other) {
String methodName = _c.getClass().getEnclosingMethod().getName();
String className = _c.getClass().getEnclosingClass().getSimpleName();
Log.d("app", className ":" methodName);
... more code here...
}
The error being on this line of code:
_c.getClass().getEnclosingMethod().getName();
I am not understanding why I am getting that error when I know _c has data and is not null?
If I call the same code but within the same class function it works as it should:
So, what am I missing here?
CodePudding user response:
The error says it all, by passing in new Object(){}.getClass()
:
if (GlobalCls.isDebugging) { GlobalCls.logData(true, new Object(){}.getClass(), null); }
you have passed a null/empty Object
which you translated into a null Class
into your method, hence it throws a NullPointerException
(which is literally on top of your huge error message).
Try passing in something else that is not a new Object()
and it should work.
Also, this question is similar to object is null even after instantiating it
CodePudding user response:
Simple error (probably from refactoring?), you are doing _c.getClass() thus getting a Class object for java.lang.Class which in turn is not a local or anonymous class and consequently getEnclosingMethod() returns null on which you then try to invoke getName().
remove the extra call:
public static void logData(Boolean isErr, Class _c, List<String> txt, String... _Other) {
String methodName = _c.getEnclosingMethod().getName();
String className = _c.getEnclosingClass().getSimpleName();
... more code here...
}