Collect: Crash when getting URI MIME type.

Created on 21 Aug 2017  路  8Comments  路  Source: getodk/collect

Software and hardware versions

Collect v1.9.1

Problem description

Crash in FormEntryActivity when getting intent URI MIME type.

Steps to reproduce the problem

Unknown, from Play Store here (requires credentials). It only affects Android 6.0 but on a range of devices.

The line that crashes is String uriMimeType = getContentResolver().getType(uri);. Either the content resolver can be null or getType dereferences uri?

Expected behavior

No crash.

Other information

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2572)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2654)
  at android.app.ActivityThread.-wrap11 (ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1488)
  at android.os.Handler.dispatchMessage (Handler.java:111)
  at android.os.Looper.loop (Looper.java:207)
  at android.app.ActivityThread.main (ActivityThread.java:5728)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:789)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:679)
Caused by: java.lang.NullPointerException: 
  at com.android.internal.util.Preconditions.checkNotNull (Preconditions.java:60)
  at android.content.ContentResolver.getType (ContentResolver.java:336)
  at org.odk.collect.android.activities.FormEntryActivity.onCreate (FormEntryActivity.java:367)
  at android.app.Activity.performCreate (Activity.java:6360)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1113)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2519)
bug good first issue help wanted

Most helpful comment

@akshay-ap As suggested by @grzesiek2010 , the only thing you need to do is to add an if statement checking whether the variable uri is null or not.
Another suggestion is to use Github Markdown syntax while writing comments on Github. You may find this useful. Markdown :smile:

All 8 comments

I think using try and catch block will fix this.
E.g.
try
{
String uriMimeType = getContentResolver().getType(uri);.
}
catch (Exception e)
{
showCustomToast(getString(R.string.error_occured), Toast.LENGTH_SHORT);
}.
Shall I do it?

@akshay-ap try/catch block is too general. We always should aim for understanding what is wrong. In that case an uri is null so we just need to add a null check. If you don't believe me you can reproduce that stacktrace changing the code manually and using Android 6.0.

You can take it of course.

@akshay-ap Are you working on it or should i fix it ?

If the bug can be fixed by just checking if uri is not null and getContentResolver() is not null then I can do it. I am new to open source contribution. Please help me if I am wrong.
t\I am thinking of doing it lke this:
ContentResolver contentResolver = getContentResolver();
String uriMimeType=null;
//Check if uri is not null.
if(uri!=null)
{
//Check if contentResolver is not null.
if(contentResolver!=null)
{
uriMimeType= getType(uri);
if(uriMimeType==null)
{
Log.e("FormEntryActivity", "getType() returned null");
showCustomToast(getString(R.string.error_occured), Toast.LENGTH_SHORT);
}
}
else
{
//contentResolver is null.
Log.e("FormEntryActivity", "getContentResolver() returned null");
showCustomToast(getString(R.string.error_occured), Toast.LENGTH_SHORT);
}
}
else
{
// uri is null.
Log.e("FormEntryActivity", "uri is null");
showCustomToast(getString(R.string.error_occured), Toast.LENGTH_SHORT);
}

as I said attached stackraces are related to uri only. The uri has a null value it's not about getContentResolver() method.

So we just need a null check for the uri.

@akshay-ap As suggested by @grzesiek2010 , the only thing you need to do is to add an if statement checking whether the variable uri is null or not.
Another suggestion is to use Github Markdown syntax while writing comments on Github. You may find this useful. Markdown :smile:

thanks for suggestions.. i am working on it..

All test are running now. Build succeeded finally.

Was this page helpful?
0 / 5 - 0 ratings