I am trying to fetch image from my firebase storage of which location is stored in a field of firestore doc, convert it into a bitmap and then set it to recycler view imageview.
This is my onBindViewHolder method :
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//This is an executor task for fetching image from firebase storage
setImageTask = Executors.newFixedThreadPool(4);
//contactToShow is a contact class object which has id, name and ph no. as fields
contactToShow = Objects.requireNonNull(contactList.get(position));
//setting name and ph no in recycler view
holder.name.setText(contactToShow.getName());
holder.phoneNumber.setText(contactToShow.getPhoneNumber());
//This is for fetching image location from firebase document for corresponding contact
db.collection("ContactData").document(String.valueOf(contactToShow.getId())).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
//this fetches location and saves it in contactProfileImageLocation
contactProfileImageLocation = documentSnapshot.getString("IMAGE LOCATION");
Log.d("AppLogs", "ImageLocFromAdapter : " + contactProfileImageLocation);
//executor class for fetching image from location got from above code
setImageTask.execute(() -> {
//imageRef is a storage reference and storage is a firebase storage instance
//These are the following Global Variables :
// FirebaseFirestore db = FirebaseFirestore.getInstance();
// FirebaseStorage storage = FirebaseStorage.getInstance();
//Contact contactToShow;
//String contactProfileImageLocation;
//ExecutorService setImageTask;
//setting imageRef
imageRef = storage.getReference(contactProfileImageLocation);
try {
//creating a local file to store downloaded image
File localFile = File.createTempFile("tempFile",".jpg");
//downloading image
imageRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
//creating bitmap out of image file on success
Bitmap bitmap = BitmapFactory.decodeFile(localFile.getAbsolutePath());
//setting bitmap to image view
holder.contactImageView.setImageBitmap(bitmap);
}
});
} catch (IOException e) {
e.printStackTrace();
}
});
};
});
}
and I am getting this error :
2022-07-31 16:46:49.872 19977-19977/com.example.contactroom E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.contactroom, PID: 19977
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
at com.example.contactroom.adapter.RecyclerViewAdapter$1$1.onSuccess(RecyclerViewAdapter.java:106)
at com.example.contactroom.adapter.RecyclerViewAdapter$1$1.onSuccess(RecyclerViewAdapter.java:102)
at com.google.firebase.storage.StorageTask.lambda$new$0$com-google-firebase-storage-StorageTask(StorageTask.java:123)
at com.google.firebase.storage.StorageTask$$ExternalSyntheticLambda12.raise(Unknown Source:6)
at com.google.firebase.storage.TaskListenerImpl.lambda$onInternalStateChanged$2$com-google-firebase-storage-TaskListenerImpl(TaskListenerImpl.java:90)
at com.google.firebase.storage.TaskListenerImpl$$ExternalSyntheticLambda2.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)
2022-07-31 16:46:49.893 19977-19977/com.example.contactroom I/Process: Sending signal. PID: 19977 SIG: 9
My Debug log is showing :
2022-07-31 16:19:58.028 19382-19382/com.example.contactroom D/AppLogs: ImageLocFromAdapter : images/1659264211278
this means image is getting saved on firebase and location for the corresponding contact is being fetched correctly and getting stored in contactProfileImageLocation
Then where i am going wrong ?
ViewHolder Class :
ContactRow.xml :