Error with calling FileProvider from DialogFragment - null object of reference, - java

I am trying to fix this error but I am kind of not seeing where it comes from.
From my fragment which creates new user I am calling another fragmentDialog (on ImageButton Click) which enables me to pick a photo from camera or gallery. Gallery works fine, but camera does not. In this line:
Uri photoUri = FileProvider.getUriForFile(globalContext, getActivity().getPackageName() + ".provider", getCameraFile());
I am getting this error:
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
All the data is displaying correctly (I check in logs).
Where cna be the issue?
Regards,
Grzegorz
EDIT:
Full stack trace:
FATAL EXCEPTION: main
Process: com.myapp.myapp, PID: 27523
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:583)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399)
at com.myapp.myapp.utils.PictureDialog.onTakePhoto(PictureDialog.java:80)
at com.myapp.myapp.utils.PictureDialog_ViewBinding$1.doClick(PictureDialog_ViewBinding.java:32)
at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
at android.view.View.performClick(View.java:5184)
at android.view.View$PerformClick.run(View.java:20893)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5940)
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:1389)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)

FileProvider cannot find the <provider> element in the manifest that has an android:authorities value equal to getActivity().getPackageName() + ".provider". Specifically, this code results in a null value for info:
final ProviderInfo info = context.getPackageManager()
.resolveContentProvider(authority, PackageManager.GET_META_DATA);
(where context is the first parameter to getUriForFile() and authority is the second parameter to getUriForFile())
Make sure that your supplied authority string matches your <provider> element.

Related

Firebase Firestore gives null pointer exception for a key which exists

I am making a chat app and want to implement a feature to get the online-offline and typing statuses for that in my adapter. I am trying to get the value from the database to display.
This is what I tried
FirebaseFirestore database = FirebaseFirestore.getInstance();
Log.d("conversationId",chatMessage.conversationId + "");
database.collection(Constants.KEY_COLLECTION_USERS)
.document(chatMessage.conversationId + "")
.addSnapshotListener((value, error) -> {
if (value.getString(Constants.KEY_AVAILABILITY).equals("0")){
binding.onlineIndicator.setVisibility(View.GONE);
binding.textRecentConversation.setText(chatMessage.message);
}else if (value.getString(Constants.KEY_AVAILABILITY).equals("1")){
binding.onlineIndicator.setVisibility(View.VISIBLE);
binding.textRecentConversation.setText(chatMessage.message);
}else if (value.getString(Constants.KEY_AVAILABILITY).equals("2")){
makeTypingText(binding, chatMessage.conversationName);
binding.onlineIndicator.setVisibility(View.VISIBLE);
}
});
And this is the error I got
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.chatverse.free, PID: 14452
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.chatverse.free.Adapters.RecentConversationsAdapter$ConversationViewHolder.lambda$setData$0$com-chatverse-free-Adapters-RecentConversationsAdapter$ConversationViewHolder(RecentConversationsAdapter.java:144)
at com.chatverse.free.Adapters.RecentConversationsAdapter$ConversationViewHolder$$ExternalSyntheticLambda3.onEvent(Unknown Source:6)
at com.google.firebase.firestore.DocumentReference.lambda$addSnapshotListenerInternal$2$com-google-firebase-firestore-DocumentReference(DocumentReference.java:504)
at com.google.firebase.firestore.DocumentReference$$ExternalSyntheticLambda2.onEvent(Unknown Source:6)
at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0$com-google-firebase-firestore-core-AsyncEventListener(AsyncEventListener.java:42)
at com.google.firebase.firestore.core.AsyncEventListener$$ExternalSyntheticLambda0.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
This is how I verified
First, in the logs, I check the value I printed for the receiver id. It's correct.
I also verified the keys, and everything is correct. I am not sure why such an error is occurring.
I got it.
I was using a static String, giving n error, but it worked when I tried using hard-coded values. Note that this gave me an emulator error but worked fine with an actual device! To make it work on an emulator, I had to try/catch for NullPointerExeption.
Thanks to #AlexMamo

nullpointerexception in getResource method

When I click back button in my app, I get a NullPointerException in getResource() method. getResource is used in the url and is called in the glideApp.
my code
private void setupPostUserImage(ImageView image, PostDetail detail) {
mUserPicture.setVisibility(View.VISIBLE);
String url = getActivity().getResources().getString(R.string.profile_image_path1) + "/" + detail.getUser_id_fk() + "/thumb/" + detail.getProfile_picture();
//Toast.makeText(getContext(),url,Toast.LENGTH_LONG).show();
GlideApp.with(image.getContext())
.load(url)
.placeholder(R.drawable.ic_avtar_male)
.into(image);
}
This is my crash report from logcat
Process: com.ATG.World, PID: 24491
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.support.v4.app.FragmentActivity.getResources()' on a null object reference
at com.ATG.World.fragments.ArticleDetailFragment2.setupPostUserImage(ArticleDetailFragment2.java:316)
at com.ATG.World.fragments.ArticleDetailFragment2.setData(ArticleDetailFragment2.java:301)
at com.ATG.World.fragments.ArticleDetailFragment2$1.onResponse(ArticleDetailFragment2.java:176)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:71)
at android.os.Handler.handleCallback(Handler.java:898)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:198)
at android.app.ActivityThread.main(ActivityThread.java:6716)
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)
12-21 19:38:17.829 24491-24491/com.ATG.World I/Process: Sending signal. PID: 24491 SIG: 9
what should I do in order to resolve this exception?
I don't have reputation to comment so I have to write answer. It looks like your fragment is not attached to activity so method getActivity() returns null. Look this answer may be it will be useful getActivity() returns null in Fragment function

NullPointerException on AsyncTask.execute()

I've been working on a single activity application that receives UDP packets, parses their message, then changes an ImageView, a TextView, and plays an alert song depending on what the message contains. I have three classes: MainActivity, ListeningService, and AndroidUDP. MainActivity contains all of the GUI implentation, AndroidUDP contains the enumeration that contains the 20 possible alert types dependent on what the UDP message contains as well as sets the TextView and ImageViews, and ListeningService contains the AsyncTask private class.
I didn't write this, an electrical engineer did and I was tasked with making it actually work. When I attempt to call the ListeningService via the AndroidUDP.startIt() method I get the error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.p3_group.v2vapp/com.p3_group.v2vapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.p3_group.v2vapp.ListeningService.continueListening()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.p3_group.v2vapp.ListeningService.continueListening()' on a null object reference
at com.p3_group.v2vapp.MainActivity.onCreate(MainActivity.java:61)
I'm sure that I'm doing something dumb like trying to call the background update from the wrong thread. I've never done this kind of thing before so any pointers would be great!
and yes, i know this code is pretty bad. I only need it functional for what we're doing, not perfect
Hi you didn't start the service at all and you are trying to bind the service in "public void doStuff(MainActivity r, IntentFilter filter)" but it will not bind, so ListeningService listeningService is null in AndroidUDP, but you are trying to call below method, but listeningService is null so it's giving null pointer exception.
public void startIt()
{
listeningService.continueListening();
}

Android Studio Weather Application Crashing

So, I have been following a YouTube tutorial for an android Weather application. The app seems to compile, however crashes whenever opened on the emulator. I'm not the most fluent programmer, however the code is understood. I can't figure out what's causing the issue though. The app is supposed to fetch weather data for the location of the hardcoded location in "MainActivity.java". The data is fetched from Yahoo! using an API.
Link for the project files: https://drive.google.com/drive/folders/0B2dQ9-JQjysVeXlqZENBZDExN0E?usp=sharing
Below is the error I get:
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mattbenson.cw_2_weather, PID: 2529
java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONObject.optInt(java.lang.String)' on a null object reference
at com.example.mattbenson.cw_2_weather.service.WeatherService$1.onPostExecute(WeatherService.java:81)
at com.example.mattbenson.cw_2_weather.service.WeatherService$1.onPostExecute(WeatherService.java:38)
at android.os.AsyncTask.finish(AsyncTask.java:667)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:684)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
E/Surface: queueBuffer: error queuing buffer to SurfaceTexture, -19
E/EGL_emulation: tid 2570: swapBuffers(487): error 0x300d (EGL_BAD_SURFACE)
W/OpenGLRenderer: swapBuffers encountered EGL error 12301 on 0x76990bb1c580, halting rendering...
Application terminated.
(Sorry, I am new to this site!)
Any help is greatly appreciated.
As StackTrace says the error is at Class WeatherService at line 81 int count = queryResults.optInt("Count"); because it is returning NULL.
Try using method optInt with fallback value queryResults.optInt("Count", 0);

java.lang.NullPointerException: Attempt to read from field 'long org.opencv.core.Mat.nativeObj' on a null object reference

I am new to openCV and android. I am trying to load an image from Resources using bitmap and process the image using open CV. It gave me this error:
java.lang.NullPointerException:
Attempt to read from field 'long org.opencv.core.Mat.nativeObj' on a null object reference
Code:
Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.a1);
((ImageView)findViewById(R.id.imageView)).setImageBitmap(bm);
Mat trainImage = new Mat(bm.getHeight(),bm.getWidth(),CvType.CV_32S);
Utils.bitmapToMat(bm,trainImage);
if(trainImage.empty()){
Log.e(TAG,"Empty image");
}
else{
Log.e(TAG,"Image Loaded");
}
Imgproc.cvtColor(trainImage, trainGray, Imgproc.COLOR_RGBA2GRAY);
Here is the Exception message:
E/SVM: Image Loaded
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pragyan.facefeatures, PID: 3308
java.lang.NullPointerException: Attempt to read from field 'long org.opencv.core.Mat.nativeObj' on a null object reference
at org.opencv.imgproc.Imgproc.cvtColor(Imgproc.java:1724)
at com.example.pragyan.facefeatures.ExtractFeature.trainSvm(ExtractFeature.java:242)
at com.example.pragyan.facefeatures.ExtractFeature.onOptionsItemSelected(ExtractFeature.java:313)
at android.app.Activity.onMenuItemSelected(Activity.java:2885)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:403)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:189)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:663)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
at android.support.v7.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:191)
at android.widget.AdapterView.performItemClick(AdapterView.java:305)
at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
at android.widget.AbsListView$3.run(AbsListView.java:3860)
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:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Can anyone tell me how to fix this. The image is loaded successfully and it still gives a NullPointerException.
I think you've not initialized the Mat trainGray. Try the following.
Mat trainGray = new Mat();
Imgproc.cvtColor(trainImage, trainGray, Imgproc.COLOR_RGBA2GRAY);

Categories