Issue
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?
Solution
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();
}
}
}
Answered By - mihanovak1024
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.