0

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Gum Naam
  • 155
  • 13

1 Answers1

1

The crashed happened here:

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)

File: URLEmbeddedTask.java line #26

You called startsWith() on a null string and the background task crashed

You can for example add some log messages and add a null check:

if(tempString != null) {
    System.out.println("String is not null.. OK scenario")
    tempString.startsWith(/*aaaa*/);
} else {
    System.out.println("String is null.. NOK scenario")
}

Update

I found the class that you are using on GitHub:

As you can see, on line #26, the url is used without checking if it is null or not:

String url = params[0];
url = ((url.startsWith(URLConstants.PROTOCOL) || url.startsWith(URLConstants.PROTOCOL_S)) ? "" : URLConstants.PROTOCOL) + url;

So, this issue is happening because you are calling the task.execute(); with a null parameter.

URLEmbeddedTask task = new URLEmbeddedTask(/*listerner*/);
task.execute(/* THIS OBJECT CAN BE NULL */);

UPDATE #2

I debug that library and I found that this is the error:

String fiverrLink1;

dRef.child(UID).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        fiverrLink1 = (String) snapshot.child("gig").getValue();
        // Only here filverrLink1 wont be null. Only after this callback is invoked.
    }
});

// Here, fiverrLink1 is null. It will be non-null only after the onDataChanged call back is invoked.
previewLink.setURL(fiverrLink1 ... ); // -> You are sending null to the library

How to Fix

Well, this depends of what you want to achive. But I would suggest to invoke setUrl only after fiverrLink1 is updated. Something like:

String fiverrLink1;

dRef.child(UID).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        fiverrLink1 = (String) snapshot.child("gig").getValue();
        previewLink.setURL(fiverrLink1 ... ); // -> Move to here
    }
});
guipivoto
  • 18,327
  • 9
  • 60
  • 75