Q: Android - Null pointer exception for this.cordova.getActivity().getApplicationContext() I have created plugin for android. Now inside the plugin I want to access application context. 
I got below solution to get context of CordovaPlugin class
Context context = this.cordova.getActivity().getApplicationContext()

But while running it throws below error
java.lang.NullPointerException: Attempt to invoke interface method 'android.app.Activity org.apache.cordova.CordovaInterface.getActivity()' on a null object reference
10-24 01:25:41.930 16692-16751/io.ionic.starter W/System.err:     at cordova.plugin.testpayment.TestPayment.<init>(TestPayment.java:25)
10-24 01:25:41.930 16692-16751/io.ionic.starter W/System.err:     at java.lang.Class.newInstance(Native Method)
10-24 01:25:41.930 16692-16751/io.ionic.starter W/System.err:     at org.apache.cordova.PluginManager.instantiatePlugin(PluginManager.java:492)
10-24 01:25:41.930 16692-16751/io.ionic.starter W/System.err:     at org.apache.cordova.PluginManager.getPlugin(PluginManager.java:169)
10-24 01:25:41.930 16692-16751/io.ionic.starter W/System.err:     at org.apache.cordova.PluginManager.exec(PluginManager.java:122)
10-24 01:25:41.930 16692-16751/io.ionic.starter W/System.err:     at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:59)
10-24 01:25:41.931 16692-16751/io.ionic.starter W/System.err:     at org.apache.cordova.engine.SystemExposedJsApi.exec(SystemExposedJsApi.java:41)
10-24 01:25:41.931 16692-16751/io.ionic.starter W/System.err:     at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
10-24 01:25:41.931 16692-16751/io.ionic.starter W/System.err:     at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:9)
10-24 01:25:41.931 16692-16751/io.ionic.starter W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-24 01:25:41.931 16692-16751/io.ionic.starter W/System.err:     at android.os.Looper.loop(Looper.java:154)

I can see error as NullPointerException.
May I know what's wrong in it?

A: Please use this 
Context context = IS_AT_LEAST_LOLLIPOP ? cordova.getActivity().getWindow().getContext() : cordova.getActivity().getApplicationContext();

