I am trying to get firebase realtime database data into the ViewHolder class but the app is crashing and logcat is not specifying the line in which the error is occurring. Below is the logcat Logcat
2022-01-26 22:04:46.007 10834-11362/com.xee.fiverrimpressions E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #6
Process: com.xee.fiverrimpressions, PID: 10834
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$4.done(AsyncTask.java:415)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.startsWith(java.lang.String)' on a null object reference
at com.nguyencse.URLEmbeddedTask.doInBackground(URLEmbeddedTask.java:26)
at com.nguyencse.URLEmbeddedTask.doInBackground(URLEmbeddedTask.java:13)
at android.os.AsyncTask$3.call(AsyncTask.java:394)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
2022-01-26 22:04:46.097 10834-10834/com.xee.fiverrimpressions I/ViewRootImpl@9fa7fd8[DashboardActivity]: MSG_WINDOW_FOCUS_CHANGED 0 1
2022-01-26 22:04:46.108 10834-11362/com.xee.fiverrimpressions I/Process: Sending signal. PID: 10834 SIG: 9
MyViewHolder
class MyViewHolder extends RecyclerView.ViewHolder {
TextView geglink, userName, userAbout;
URLEmbeddedView previewLink;
View v;
DatabaseReference dRef;
FirebaseDatabase firebaseDatabase;
FirebaseAuth fAuth;
String UID;
String fiverrLink1;
public MyViewHolder(@NonNull @NotNull View itemView) {
super(itemView);
previewLink = itemView.findViewById(R.id.uev);
geglink = (TextView) itemView.findViewById(R.id.rcvGigLink);
userName = (TextView) itemView.findViewById(R.id.rcvName);
userAbout = (TextView) itemView.findViewById(R.id.rcvAbout);
v = itemView;
fAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();
dRef = FirebaseDatabase.getInstance().getReference("Users");
if(fAuth.getCurrentUser() != null){
UID = FirebaseAuth.getInstance().getCurrentUser().getUid();
}
dRef.child(UID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
fiverrLink1 = (String) snapshot.child("gig").getValue();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
previewLink.setURL(fiverrLink1, new URLEmbeddedView.OnLoadURLListener() {
@Override
public void onLoadURLCompleted(URLEmbeddedData data) {
previewLink.title(data.getTitle());
previewLink.description(data.getDescription());
previewLink.host(data.getHost());
previewLink.thumbnail(data.getThumbnailURL());
previewLink.favor(data.getFavorURL());
}
});
I think the error is in addValueEventListener as if I write fiverrLink1 = "https://www.youtube.com" outside addValueEventListener the app works perfectly. Please help what is causing the error and how to solve it. Thank You