Backendless Support
 
Waiting for user's response

Android restoreAuthKeysFromPreferences - java.lang.NullPointerException

After migration to Backendless 4.x I did also needed Android changes with new SDK. Currently I have few reported issues of null pointer exception. Any idea or help? Thank you.

  1. java.lang.ExceptionInInitializerError: at com.backendless.Invoker.invokeSync(Invoker.java) at <OR> com.backendless.Invoker.invokeSync (Invoker.java) at com.backendless.Invoker$1.run (Invoker.java) at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:607) at java.lang.Thread.run (Thread.java:762)Caused by: java.lang.NullPointerException: at com.backendless.AndroidBackendlessPrefs.restoreAuthKeysFromPreferences (AndroidBackendlessPrefs.java) at com.backendless.AndroidBackendlessPrefs.getAuthKeys (AndroidBackendlessPrefs.java) at com.backendless.AndroidBackendlessPrefs.getApplicationId (AndroidBackendlessPrefs.java) at com.backendless.Backendless.getApplicationId (Backendless.java) at com.backendless.AndroidHeadersManager.<init> (AndroidHeadersManager.java) at com.backendless.AndroidHeadersManager.getInstance (AndroidHeadersManager.java) at com.backendless.HeadersManager.<clinit> (HeadersManager.java)

Leave a Comment

Comments (15)

photo
1

Hi Martin,

Have you called Backendless.initApp before that?

photo
1

Hello Segey,

yes I call init in MainActivity onCreate() method.

  1. Backendless.initApp(getApplicationContext(), API_ID, API_KEY);

I run my code more than 6months on Backendless 3.x and I got this error after migration SDK to new version of Backendless 4.x.

Any Idea why what cause this NullPointer? My app use background service where calls Backendless.

I use SDK from following Gradle dependency

  1. compile 'com.backendless:backendless:4.0.3'

photo
1

As far as I can see from the stacktrace in the sources, the NullPointerException happens in AndroidBackendlessPrefs.restoreAuthKeysFromPreferences method, most probably because of `sharedPreferences` variable being null. This variable is initialized in AndroidBackendlessPrefs.onCreate method, which in its turn is called from Backendless.initApp method. This is why I asked whether you call initApp before using any other method.

It is also possible that the background service you mentioned touches the AndroidBackendlessPrefs class somehow before the SDK is initialized with Backendless.initApp method.

Anyway we need more information in order to help you further, so here's what you can do in order to track the problem:

  1. Minimize your code in order to create a minimal verifiable sample, which would reproduce the problem. Then you can send it to us and we'll debug the NPE and its cause.
  2. Or try to debug the problem by yourself - the sources of our Android SDK are open and are available on GitHub. You can attach them to your project and set the breakpoint on the line producing NPE and see from where and when it is called.

photo
1

Hello Sergey,

I will try to debug the app, unfortunately this crash happenes adhoc.

In Backendless source code I see following:

  1. String version = sharedPreferences.getString( Type.VERSION.name64(), null );

There is no VERSION for Backendless 4.x anymore. This code should be cause of null pointer, because will never set authKeys for getApplicationId() calls.

I have only few following calls to Backendless from my app:

  • from application onCreate

  1. Backendless.initApp(getApplicationContext(), API_ID, API_KEY);from background service

  • read/write/messaging from background service

  1. Backendless.Persistence.of(LostDevice.class).find(dataQuery, new AsyncCallback<List<LostDevice>>() {
  2. Backendless.Persistence.of(LostDevice.class).find(dataQuery, new AsyncCallback<List<LostDevice>>() {
  3. Backendless.Messaging.registerDevice(GOOGLE_PUSH_MESSAGE_SERVER_API_ID, "default", new AsyncCallback<Void>() {
  4. Backendless.Messaging.getRegistrations(new AsyncCallback<DeviceRegistration>() {
  5. Backendless.Persistence.save(lostDevice, new AsyncCallback<LostDevice>() {

photo
1

Actually, you're right - there's a bug here. I've created a pull request with a fix: https://github.com/Backendless/Android-SDK/pull/291

Though still I don't see how this one would produce an NPE in this method: in case there's no version, the method merely does nothing (does not initialize authKeys variable), but it doesn't throw a NullPointerException because of missing version.

photo
1

Hi Sergey, it cause a NPE later. See method getApplicationId() where getAuthKeys() is exactly null because of VERSION ;-)

photo
1

But the stacktrace states that NPE happens in AndroidBackendlessPrefs.restoreAuthKeysFromPreferences method, isn't it?

  1. java.lang.ExceptionInInitializerError:
  2. at com.backendless.Invoker.invokeSync(Invoker.java) at <OR> com.backendless.Invoker.invokeSync (Invoker.java)
  3. at com.backendless.Invoker$1.run (Invoker.java)
  4. at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1133)
  5. at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:607)
  6. at java.lang.Thread.run (Thread.java:762)
  7. Caused by: java.lang.NullPointerException:
  8. at com.backendless.AndroidBackendlessPrefs.restoreAuthKeysFromPreferences (AndroidBackendlessPrefs.java)
  9. at com.backendless.AndroidBackendlessPrefs.getAuthKeys (AndroidBackendlessPrefs.java)
  10. at com.backendless.AndroidBackendlessPrefs.getApplicationId (AndroidBackendlessPrefs.java)
  11. at com.backendless.Backendless.getApplicationId (Backendless.java)
  12. at com.backendless.AndroidHeadersManager.<init> (AndroidHeadersManager.java)
  13. at com.backendless.AndroidHeadersManager.getInstance (AndroidHeadersManager.java)
  14. at com.backendless.HeadersManager.<clinit> (HeadersManager.java)

photo
1

Yes, you are right, but this is a stacktrace from production - from google play console after ProGuard obfuscation. I am not sure how much of stacktrace was removed by google console.

But from Backendless source code I see possible NPE for getApplicationId() calls because of missing VERSION type.

photo
1

OK, then we'll try to push the updated library to Maven Central, and will notify you here when it's ready.

photo
1

Great, thank you so much ;-)

photo
1

Do you have any news about the release date of fixed version? Thank you.

photo
1

We have pushed it to Maven yesterday, but it hasn't yet appeared on their repositories - it usually takes a day or two.

I'm monitoring the repos at http://mvnrepository.com/artifact/com.backendless/backendless and https://search.maven.org/#search%7Cga%7C1%7Ca%3A%22backendless%22 - you could monitor them too, if you want. Anyway, as soon as the new version appears I'll notify you here.

photo
1

Version 4.0.4 is already available: https://search.maven.org/#artifactdetails%7Ccom.backendless%7Cbackendless%7C4.0.4%7Cjar

Though it still hasn't synchronized with mvnrepository.com, I think you can already use it.

photo
1

Hey Sergey, unfortunately after upgrade to new version I still have reported app crash on NPE. Any idea what should cause this NPE?

  1. java.lang.ExceptionInInitializerError: at com.backendless.Invoker.invokeSync(Invoker.java)
  2. at <OR> com.backendless.Invoker.invokeSync (Invoker.java)
  3. at com.backendless.Invoker$1.run (Invoker.java)
  4. at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1133)
  5. at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:607)
  6. at java.lang.Thread.run (Thread.java:762)
  7. Caused by: java.lang.NullPointerException:
  8. at com.backendless.AndroidBackendlessPrefs.restoreAuthKeysFromPreferences (AndroidBackendlessPrefs.java)
  9. at com.backendless.AndroidBackendlessPrefs.getAuthKeys (AndroidBackendlessPrefs.java)
  10. at com.backendless.AndroidBackendlessPrefs.getApplicationId (AndroidBackendlessPrefs.java)
  11. at com.backendless.Backendless.getApplicationId (Backendless.java)
  12. at com.backendless.AndroidHeadersManager.<init> (AndroidHeadersManager.java)
  13. at com.backendless.AndroidHeadersManager.getInstance (AndroidHeadersManager.java)
  14. at com.backendless.HeadersManager.<clinit> (HeadersManager.java)

photo
1

Hi Martin,

Unfortunately, there's nothing more we can currently find. The situation remains the same as I described in my comment here: http://support.backendless.com/topic/java-lang-nullpointerexception#comment-33038

photo