Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Null Pointer Exception in Camera App

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I keep getting this error and I'm not sure how to fix it. Please help.

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.example.user.myapplication.MainActivity.openCamera(MainActivity.java:66)
                                                                                 at com.example.user.myapplication.MainActivity.access$000(MainActivity.java:27)
                                                                                 at com.example.user.myapplication.MainActivity$1.onClick(MainActivity.java:48)
                                                                                 at android.view.View.performClick(View.java:5052)
                                                                                 at android.view.View$PerformClick.run(View.java:20162)
                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:135)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5753)
                                                                                 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:1405)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)



 
Marshal
Posts: 7604
522
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think exact specified problem is in the code part you posted. It might contains other problems, though.

Can't you understand what null pointer exception is?
 
Liutauras Vilda
Marshal
Posts: 7604
522
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I think I was wrong.

Check whether "photoURI" isn't null. It might be.
 
Rancher
Posts: 4553
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like there isn't a FileProvider defined for the app?  Though I will say it's not the most useful error message...

The FileProvider docs talk about setting one up in the application manifest, so can you show us what yours looks like to check it's OK?
 
nadeera rajapakse
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've added this to the manifest to solve the problem.







But I get this error now.

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
                                                                                   at com.example.user.myapplication.MainActivity.onActivityResult(MainActivity.java:80)



 
Dave Tolls
Rancher
Posts: 4553
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the requestCode to make sure that's the correct Intent.
See the docs for Activity here for what that represents.
 
nadeera rajapakse
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding   if(data != null) {} solved the problem.



How do I solve this warning? And the pictures taken are not loaded onto the ImageView.

 W/ContextImpl: Failed to ensure directory: /storage/extSdCard/Android/data/com.example.user.myapplication/files

 
Dave Tolls
Rancher
Posts: 4553
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not what the Android docs say, though.

The link I posted shows that you not only need to check the resultCode, you also need to check the requestCode to ensure it is the Intent you are expecting.

As for the new error, that error text looks incomplete to me.
 
nadeera rajapakse
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case I still have no idea what's wrong.

03-23 16:52:08.382 22234-22234/com.example.user.myapplicationcamera E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.example.user.myapplicationcamera, PID: 22234
                                                                                     java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.user.myapplicationcamera/com.example.user.myapplicationcamera.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
                                                                                         at android.app.ActivityThread.deliverResults(ActivityThread.java:3949)
                                                                                         at android.app.ActivityThread.handleSendResult(ActivityThread.java:3992)
                                                                                         at android.app.ActivityThread.access$1400(ActivityThread.java:168)
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                         at android.os.Looper.loop(Looper.java:135)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5753)
                                                                                         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:1405)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
                                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
                                                                                         at com.example.user.myapplicationcamera.MainActivity.onActivityResult(MainActivity.java:82)
                                                                                         at android.app.Activity.dispatchActivityResult(Activity.java:6375)
                                                                                         at android.app.ActivityThread.deliverResults(ActivityThread.java:3945)
                                                                                         at android.app.ActivityThread.handleSendResult(ActivityThread.java:3992) 
                                                                                         at android.app.ActivityThread.access$1400(ActivityThread.java:168) 
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                         at android.os.Looper.loop(Looper.java:135) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5753) 
                                                                                         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:1405) 
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 



 
Dave Tolls
Rancher
Posts: 4553
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out of curiosity, where are you running this?
 
nadeera rajapakse
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Android Studio
 
Dave Tolls
Rancher
Posts: 4553
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

nadeera rajapakse wrote:Android Studio


Ah, so you're testing with a virtual device?
Are you sure the "camera" stuff functions for that?

I'm beginning to suspect that might be the issue.
 
nadeera rajapakse
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, I'm using my phone to run it instead of the emulator.
 
Dave Tolls
Rancher
Posts: 4553
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

nadeera rajapakse wrote:Nope, I'm using my phone to run it instead of the emulator.



OK, so you're running it on your phone?
 
nadeera rajapakse
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup
 
Dave Tolls
Rancher
Posts: 4553
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm.
Well, I'm at a loss why it isn't supplying the Intent.

Could it be something to do with the target for the image file?
But then that all looks how I would expect (though that is no guarantee).
In any case, I would have expected an error from the camera software if there was an issue there.
 
I'm not sure if I approve of this interruption. But this tiny ad checks out:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic