Collect: java Null Pointer Exception on getVersionedAppName() method thus causing app to crash

Created on 27 Feb 2017  路  16Comments  路  Source: getodk/collect

Software and hardware versions

Collect v1.x.x, Android v5.1.1, device used...

Problem description

App Crashes as soon as it installs.

Steps to reproduce the problem

Expected behavior

Other information

Things you tried, stack traces, related issues, suggestions on how to fix it...

bug good first issue needs reproduction

Most helpful comment

Can someone please include the stack trace?

All 16 comments

Just updated the code
here is the PR: https://github.com/opendatakit/collect/pull/468

Can you please edit your issue description to include the stack trace and details about the device/emulator that you are running the app on?

Hi @lognaturel
Problem
App Crashes as soon as it installs because of attempting to invoke null object reference as given in below link more details.after trying many times to upload images by dragging on GitHub.I have created HTML file.
Please See.
https://vikasdesale.github.io/issue_466.html

sorry for the inconvenience!

Thanks and Regards,
Vikas Desale

@vikasdesale Image upload probably won't work because S3 is down. See https://twitter.com/awscloud.

I think this NPE is likely because the build scripts require that you have tags and you might not have fetched tags.

@thegenuinegourav @vikasdesale What happens if you run git tag. Do you see a bunch of tags? My guess is no. What happens then if you run git fetch --tags and then try to run the app. Does the NPE go away? My guess is yes.

If my guess is correct, then the best fix is to change the git command in build.gradle to show a uniquely abbreviated commit object as fallback

case 'windows':
    commandLine 'cmd', '/c', 'git', 'describe', '--tags', '--dirty', '--always'
    break
case 'unix':
    commandLine 'git', 'describe', '--tags', '--dirty', '--always'
    break

Thank you @yanokwa :slightly_smiling_face: , your guess is correct sir that initial I haven't seen any tag.
1.I have fetched tags then reopened and rebuild app still shows NPE.
2.after that I edited build.Gradle and rebuild app still shows NPE.
Please look at this sir.
Solution:
I have only checked for versionName not null before appending the value.

Thanks and Regards
Vikas Desale

Can someone please include the stack trace?

Thank you @lognaturel and @yanokwa for looking back to this issue.here is the stack trace and device screen
untitled2

untitled

Should I send a pull request? :slightly_smiling_face:

It stills giving NPE after those certain updates @yanokwa

@thegenuinegourav @vikasdesale Interesting, that stack trace isn't useful because the problem is occurring in the Gradle function. Are you sure that you're both running the gradle task and not the class directly?

@thegenuinegourav @vikasdesale Any updates on this?

I was able to reproduce this issue as well. It takes place when I download the code - if I use git clone everything is ok. I think we should catch that null.

03-13 11:21:36.520 18178-18178/org.odk.collect.android E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.odk.collect.android, PID: 18178
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.odk.collect.android/org.odk.collect.android.activities.MainMenuActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.replaceFirst(java.lang.String, java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.replaceFirst(java.lang.String, java.lang.String)' on a null object reference
at org.odk.collect.android.application.Collect.getVersionedAppName(Collect.java:179)
at org.odk.collect.android.activities.MainMenuActivity.onCreate(MainMenuActivity.java:130)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)聽
at android.app.ActivityThread.access$900(ActivityThread.java:177)聽
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)聽
at android.os.Handler.dispatchMessage(Handler.java:102)聽
at android.os.Looper.loop(Looper.java:145)聽
at android.app.ActivityThread.main(ActivityThread.java:5951)聽
at java.lang.reflect.Method.invoke(Native Method)聽
at java.lang.reflect.Method.invoke(Method.java:372)聽
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)聽
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)聽

@grzesiek2010 You said it has something to do with git not being found in that directory that you downloaded the code to, right?

The challenge with catching the null is... then what?! I suppose we could give a nicer message that explains git is missing?

Yes git s missing. I thought about catching null only to avoid crash but ok I don't mind we can give a messega of course as well.

Let's catch the null and print a message about git not being in the path.

Hi, if its still open can i work on it ? @lognaturel @yanokwa

Hi everyone, I've figured this out. The bug appears because the getPackageManager() methods works when called from an activity class. Since getVersionedAppName() method is made in an non-activity class the context of getPackageManager() method is null, hence the NPE.
The alternative best way is to use the BuildConfig class like this

String versionName = BuildConfig.VERSION_NAME;

This works just fine. I will be sending a PR for the same.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rcovane picture rcovane  路  3Comments

jsoppela picture jsoppela  路  4Comments

nribeka picture nribeka  路  4Comments

JeanBaisez picture JeanBaisez  路  4Comments

mateusmedeiros picture mateusmedeiros  路  4Comments