0

In Kotlin, I'm building an app that uses the camera. However, it crashes whenever I click the camera button either in emulator or on actual device. I get the error below.

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference

This error-type is non-specific and has been asked a number of times by Java (not kotlin) lang users. The solution has typically been name mismatch between auth in manifest file and usage in code function call, however none of the proposed solutions have worked for me.

Here is the snippet in my code where I make the 'null reference' call:

var currentPath: String? = null
val TAKE_PICTURE = 1
val SELECT_PICTURE = 2

fun dispatchCameraIntent() {
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    if(intent.resolveActivity(packageManager) != null){
    var photoFile: File? = null
    try {
        photoFile = createImage()
    }catch (e: IOException){
        e.printStackTrace()
    }
    if (photoFile != null){
        // you must create a content provider matching the authority
        var photoUri = FileProvider.getUriForFile(this, "com.my_company.my_app.fileprovider", photoFile)
        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
        startActivityForResult(intent, TAKE_PICTURE)
    }
   }
}

    fun createImage(): File{
        val timeStamp =       SimpleDateFormat("yyyyMMMM_HHmmss").format(Date())//getDateTimeInstance()
        val imageName = "JPEG_"+timeStamp+"_"
        var storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
        var image = File.createTempFile(imageName, ".jpg", storageDir)
        currentPath = image.absolutePath
        return image
    }

My xml file_paths file is at res>xml>file_paths.xml and is as follows:

<?xml version="1.0" encoding="utf-8"?>

<paths     xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
    name="my_pictures"
        path="SDCARD/Android/data/com.my_company.my_app/files/Pictures"/>

Here is my full AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest     xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my_company.my_app">

<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera2" android:required="false"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity     android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity      android:name=".EmailSignIn" />
    <activity android:name=".RegisterUser" />
    <activity android:name=".ForgotPassword" />
    <activity android:name=".ViewController">
    </activity>


    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.my_company.my_app.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"/>
    </provider>


</application>

Here is the trace:

08-04 18:03:51.830 28205-28205/com.my_company.my_app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.my_company.my_app, PID: 28205
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
    at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:584)
    at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:558)
    at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
    at com.my_company.my_app.ViewController.dispatchCameraIntent(ViewController.kt:91)
    at com.my_company.my_app.ViewController$onCreate$2.onClick(ViewController.kt:47)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Aug 05 '18 at 00:13
  • If you want hints on how to solve it, read: https://stackoverflow.com/a/24347569/139985 – Stephen C Aug 05 '18 at 01:23

1 Answers1

0

This is the code that is causing the problem. It is in the FileProvider class.

    final ProviderInfo info = context.getPackageManager()
            .resolveContentProvider(authority, PackageManager.GET_META_DATA);
    final XmlResourceParser in = info.loadXmlMetaData(
            context.getPackageManager(), META_DATA_FILE_PROVIDER_PATHS);

The stacktrace clearly shows that the NPE is happening for the loadXmlMetaData call, and it is happening because info is null. (There is only one call-site for that method in the FileProvider class ...)

This evidence points to something being wrong with the way you have defined your FileProvider.

  • Check / recheck the documentation.
  • Check for typos or mismatches in the manifest, etcetera.
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks for your response, Stephen. I am working in Kotlin and did not explicitly define the FileProvider class. I reviewed the documentation, but I still can't find what the issue is. Could it have something to do with my variable declarations, which I have now added to my code snippet show above? – VeryVeryGoodNews Aug 06 '18 at 02:05
  • The documentation talks about the `` config, and also about a security setting. Check those. They would apply to Kotlin ... – Stephen C Aug 06 '18 at 02:27
  • Checked. No luck. Anyone else know what the problem could be? – VeryVeryGoodNews Aug 08 '18 at 16:18
  • The issue was a typo in my authorities entry in AndroidManifest.xml – VeryVeryGoodNews Aug 18 '18 at 01:26