2

I have no idea how to work out where this null pointer exception is occurring.

The crash has only happened once (so far) on a user's physical device as advised by Google Play - I haven't been able to reproduce it in a debug environment.

The Stack Trace that Google Play gives me seems a bit vague (despite me having loaded a mapping file for the release):

java.lang.NullPointerException: 
  at com.nooriginalthought.amalfi.getShortURL.a (getShortURL.java:11)
  at com.nooriginalthought.amalfi.getShortURL.onPostExecute (getShortURL.java:2)
  at android.os.AsyncTask.finish (AsyncTask.java:695)
  at android.os.AsyncTask.access$600 (AsyncTask.java:180)
  at android.os.AsyncTask$InternalHandler.handleMessage (AsyncTask.java:712)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:193)
  at android.app.ActivityThread.main (ActivityThread.java:6806)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:547)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:873)

All I can see is that the exception appears to be being thrown by the onPostExecute function in my getShortURL class (this is an async task that calls the bitly API).

The code works fine and only a single crash has been reported.

The code in onPostExecute is very simple (incl source code line numbers):

1320    @Override
1321    protected void onPostExecute(String shortURL) {
1322        super.onPostExecute(shortURL);
1323        mainActivityWeakReference.get().shortURLreturn(shortURL);
1324    }

(shortURLreturn is defined within my MainActivity class)

How can I work out what actually threw this error?

Fat Monk
  • 2,077
  • 1
  • 26
  • 59
  • That's a tough one. The '.a' is a sure sign getShortURL was obfuscated - but evidently only partially (since onPostExecute is listed) - perhaps you're using the wrong map to deobfuscate? Can't the WeakReference.get return null whenever the GC decides? –  Nov 12 '18 at 18:04
  • `@NonNull String shortURL` – Martin Zeitler Nov 12 '18 at 18:57

1 Answers1

1

There's not much code posted, so there'll be mostly guessing.

How sure are you that the mainActivityWeakReference isn't null since it's a weak reference? Check this link on how to use weak reference activities link. Something like:

@Override
protected void onPostExecute(String shortURL) {
  MainActivity mainActivity = mainActivityWeakReference.get();
  if (mainActivity != null) {
     mainActivity.shortURLreturn(shortURL);
  }
}

Also, for that kind of action, you don't actually need a weak reference to the activity (of course, only if you don't need any methods that actually belong to the Android Activity class). If you only need your public methods that you have implemented in your MainActivity, you could make an Interface, for example:

interface ShortUrlReceiveCallback {
   void onReceive(String shortURL);
}

and then you would just have that reference saved in the constructor of getShortURL class (note that classes start with uppercase) and later on call in onPostExecute -> shortUrlReceiveCallback.onReceive(shortURL); and have your MainActivity implement that ShortActivityCallback.

For my 2nd point, bare in mind that onPostExecute happens on a different thread (IO/background thread) than MainActivity (UiThread), if you are doing anything afterwards that's supposed to be on main thread or thread safe (variables setting should be thread safe; try using locks if you aren't for example:

class MainActivity {
   private String mainShortUrl;
   private Lock urlReturnLock = new ReentrantLock();
   ...
   public void shortURLReturn(String shortUrl) {
      urlReturnLock.lock();
      try {
         mainShortUrl = shortUrl;
      } finally {
         urlReturnLock.unlock();
      }
   }
}
mihanovak1024
  • 246
  • 2
  • 10
  • So, if I understand correctly, I should be able to avoid the null pointer by simply checking that my week reference still exists (is not null) before carrying out anything useful in `onPostExecute`? And the only reason the week reference would be null is it the garbage collector had destroyed it out the user has had like the process of something similar - in which case my async task would have nothing to return to anyway, so not running my return function would be fine. Have I understood that correctly? (And then the `interface` stuff is interesting for another time, thanks!) – Fat Monk Nov 12 '18 at 21:54