BruceJo
BruceJo

Reputation: 771

NullPointerException: Attempt to invoke virtual method 'void org.apache.cordova.CordovaPlugin.privateInitialize

I was working away on my cordova plugin doing some refactoring and I started to get this crash on startup:

--------- beginning of crash
12-12 21:42:29.791  4693  4693 E AndroidRuntime: FATAL EXCEPTION: main
12-12 21:42:29.791  4693  4693 E AndroidRuntime: Process: xyz.meris.app, PID: 4693
12-12 21:42:29.791  4693  4693 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{xyz.meris.app/xyz.meris.app.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void org.apache.cordova.CordovaPlugin.privateInitialize(java.lang.String, org.apache.cordova.CordovaInterface, org.apache.cordova.CordovaWebView, org.apache.cordova.CordovaPreferences)' on a null object reference
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3162)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3305)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1993)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:106)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:216)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:7266)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
12-12 21:42:29.791  4693  4693 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void org.apache.cordova.CordovaPlugin.privateInitialize(java.lang.String, org.apache.cordova.CordovaInterface, org.apache.cordova.CordovaWebView, org.apache.cordova.CordovaPreferences)' on a null object reference
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at org.apache.cordova.PluginManager.getPlugin(PluginManager.java:171)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at org.apache.cordova.PluginManager.startupPlugins(PluginManager.java:97)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at org.apache.cordova.PluginManager.init(PluginManager.java:86)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at org.apache.cordova.CordovaWebViewImpl.init(CordovaWebViewImpl.java:117)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at org.apache.cordova.CordovaActivity.init(CordovaActivity.java:149)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at org.apache.cordova.CordovaActivity.loadUrl(CordovaActivity.java:224)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at xyz.meris.app.MainActivity.onCreate(MainActivity.java:39)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.Activity.performCreate(Activity.java:7353)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.Activity.performCreate(Activity.java:7344)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3142)
12-12 21:42:29.791  4693  4693 E AndroidRuntime:    ... 11 more
12-12 21:42:29.826  4693  4693 I Process : Sending signal. PID: 4693 SIG: 9

I hunted around and found some similar questions:

But the solutions did not work for me...

Upvotes: 2

Views: 6854

Answers (2)

Uzbekjon
Uzbekjon

Reputation: 11813

I had the same problem in an Ionic application. The problem was caused by missing Java packages in platforms/android/app/src/main/java of some plugins that were defined in config.xml. Usually the reason for that is adding/removing cordova plugins manually.

To solve the problem, see a list of all <feature> tags in your config.xml file and make sure the packages (folder & files) defined in its name attribute present in platforms/android/app/src/main/java.

<feature name="Device">
    <param name="android-package" value="org.apache.cordova.device.Device" />
</feature>
<feature name="SplashScreen">
    <param name="android-package" value="org.apache.cordova.splashscreen.SplashScreen" />
    <param name="onload" value="true" />
</feature>
<!-- etc. -->

To make search easier, look for the ClassNotFoundException in Android app's log files.

W/System.err: java.lang.ClassNotFoundException: org.apache.cordova.file.FileUtils

Upvotes: 0

BruceJo
BruceJo

Reputation: 771

I got some clues from the other questions and because I was refactoring I had been working with the plugin.xml file.

I noticed that I had this difference from other plugin.xml files:

      <feature name="CDVProcessLog">
        <param name="android-package" value="xyz.meris.processlog.CDVProcessLog" />
        <param name="onload" value="true" />
      </feature>
      <feature name="ProcessLog">
        <param name="android-package" value="xyz.meris.processlog.ProcessLog" />
+       <param name="onload" value="true" />
      </feature>

Because I copied the <feature> tag and just filled it in I had copied the onload parameter. Once I removed the duplicate onload parameter the crash stopped happening.

EDIT: erp. Turns out I had another error that was causing this issue.

Removing the onload param lazy loaded ProcessLog. So I would get a null pointer error later.

My real problem was that my .js file did not get updated with my CordovaPlugin class name. I had changed it from ProcessLogto CDVProcessLog. ProcessLog now had the specifics for processing the log and CDVProcessLog had mostly cordova glue to call my methods in ProcessLog.

My .js file needed to change:

  return new Promise((resolve, reject) => {
-    exec(resolve, reject, 'ProcessLog', func, [...args]);
+    exec(resolve, reject, 'CDVProcessLog', func, [...args]);
  });

Upvotes: 1

Related Questions