0

I'm following this tutorial on how to Retrieve Uploaded Images from Firebase Storage to show in recyclerview. But when I run my app I don't see the uploaded images instead my app crashes and the error keeps pointing at this certain line that contains this code ImageUploadInfo imageUploadInfo = postSnapshot.getValue(ImageUploadInfo.class);but its not really explaining why its an error. I'm not sure whats going on can someone please...pretty please help me I've been looking for answers on this all week and haven't gotten any. Thanks an advance

Errors:

FATAL EXCEPTION: main
Process: com.myapp.Gogo, PID: 22974
com.google.firebase.database.DatabaseException: Found two getters or fields 
with conflicting case sensitivity for property: imageurl

2020-03-05 11:06:36.238 22974-22974/com.myapp.Gogo
E/RecyclerView: No adapter attached; skipping layout

Adapter Class:

public class AdapterOne extends RecyclerView.Adapter<AdapterOne.ViewHolder> {
    Context context;
    List <ImageUploadInfo> ImageUploadInfoList;

    public AdapterOne(Context c, List<ImageUploadInfo> TempList){
        this.ImageUploadInfoList=TempList;
        this.context=c;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ImageUploadInfo imageUploadInfo=ImageUploadInfoList.get(position);

        //loading image with glide libary
        Glide.with(context).load(imageUploadInfo.getImageUrl()).into(holder.imageView);
    }

    @Override
    public int getItemCount() {
        return ImageUploadInfoList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView imageView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView=(ImageView) itemView.findViewById(R.id.imageview);
        }
    }
}

ImageUpload.class:

public class ImageUploadInfo {
    public String imageURL;

    public ImageUploadInfo( String url) {
        //this.imageName = name;
        this.imageURL= url;
    }

    public String getImageUrl() {
        return null;
    }
}

This is the main page:

    if(firebaseUser!=null) {
        String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference("Users");
        DatabaseReference uidRef = rootRef.child("images").child(uid);

        uidRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                    ImageUploadInfo imageUploadInfo = postSnapshot.getValue(ImageUploadInfo.class);
                    list.add(imageUploadInfo);
                }

                adapter1 = new AdapterOne(getApplicationContext(), list);
                recyclerView.setAdapter(adapter1);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {}
        });
    }
Sammy T
  • 1,924
  • 1
  • 13
  • 20
  • If the app crashes, there is a stack trace. Please look that up on logcat, and add it to your question. – Alex Mamo Mar 05 '20 at 16:05
  • @AlexMamo it says the same thing no explanation ProfileActivity$1.onDataChange(ProfileActivity.java:77) – Supercoder54551 Mar 05 '20 at 16:09
  • That line is what marshalls your data so there might be something wrong with some type you receive in response perhaps and yes you should post the stacktrace, what if someone finds something you didn't notice perhaps. Also [check this answer](https://stackoverflow.com/a/47188249/11546258) which might help you locate the issue. – ljk Mar 05 '20 at 16:13
  • The log should show what kind of error it is (i.e. NullPointerException, etc). – Sammy T Mar 05 '20 at 16:14
  • Please edit your question and add the entire error. Please also respond with @AlexMamo – Alex Mamo Mar 05 '20 at 16:18
  • @SammyT okay i see FATAL EXCEPTION: main Process: com.myapp.Gogo, PID: 22974 com.google.firebase.database.DatabaseException: Found two getters or fields with conflicting case sensitivity for property: imageurl – Supercoder54551 Mar 05 '20 at 16:22
  • @AlexMamo I foound the error i posted it – Supercoder54551 Mar 05 '20 at 16:24
  • There you go see how the stack trace had the issue you are getting. Now if you can post your `ImageUploadInfo.class` that might be even easier else if you can't try if [this](https://stackoverflow.com/a/43310925/11546258) was the case. – ljk Mar 05 '20 at 16:34
  • @ljk I posted the imageUpload class – Supercoder54551 Mar 05 '20 at 16:40
  • The error you're getting on your RecyclerView is because you're trying to set the Adapter in an asynchronous callback. You should set the adapter inside of `onCreate` see [this answer](https://stackoverflow.com/a/58669100/6253847) – Sammy T Mar 05 '20 at 17:12
  • @Supercoder54551 Please also add the content of your `ImageUploadInfo` class. Please also respond with @AlexMamo – Alex Mamo Mar 05 '20 at 18:08

1 Answers1

0

Change your model class like this and see if this works out for you. Marking it as private makes firebase only find the getter/setter and try to adhere to Java convention of camelCase while naming.

public class ImageUploadInfo {

    private String imageUrl; // main change is this should be private

    private ImageUploadInfo() {} // default constructor that takes no arguments

    public ImageUploadInfo(String imageUrl) {
        this.imageUrl= imageUrl;
    }

    public String getImageUrl() {
        return imageUrl;  // and why are you returning null here
    }
}

For the adapter issue. You need to set it from the MainThread so, Move the lines from your onDataChange method

adapter1 = new AdapterOne(getApplicationContext(), list);
recyclerView.setAdapter(adapter1);
adapter1.notifyDataSetChanged;

to the onCreate() method of your activity, and in onDataChange after adding to list initialized with list = new ArrayList<>(); only use

adapter1.notifyDataSetChanged

Let me know if this was able to solve your issue.

ljk
  • 1,496
  • 1
  • 11
  • 14
  • I just fixed that part but now the error is telling me 2020-03-05 11:47:56.329 24587-24587/com.myapp.Gogo E/RecyclerView: No adapter attached; skipping layout – Supercoder54551 Mar 05 '20 at 17:12
  • try creating the adapter and setting it to recyclerview in your activity ```onCreate```, make sure list is initialized with new ArrayList to avoid NPE and then after data is added to list, call adapter's ```notifyDataSetChanged()``` – ljk Mar 05 '20 at 17:20
  • @lijk I'm sorry can you please show me how my code supposed to look cause I'm not sure if I did it right because when I run the app I get nothing and I still get that same message – Supercoder54551 Mar 05 '20 at 17:34
  • Move these lines ```adapter1 = new AdapterOne(getApplicationContext(), list);``` ```recyclerView.setAdapter(adapter1);``` ```adapter1.notifyDataSetChanged``` to ```onCreate```. and in ```onDataChange``` after adding to list only use ```adapter1.notifyDataSetChanged``` – ljk Mar 05 '20 at 17:45
  • Now my app is crashing because it says Attempt to invoke interface method 'int java.util.List.size()' on a null object reference public int getItemCount() { return ImageUploadInfoList.size(); } – Supercoder54551 Mar 05 '20 at 18:09
  • Thats what I mentioned in an earlier comment " make sure list is initialized with new ArrayList to avoid NPE" so before you set the adapter in ```onCreate``` make sure you have initialized your list as ```list = new ArrayList<>();``` The tutorial you linked to already does that – ljk Mar 05 '20 at 18:11
  • okay I finally fixed it, now I no longer get any errors but I don't see any images being displayed. – Supercoder54551 Mar 05 '20 at 19:28
  • I seem to have missed adding the default constructor in my code, updated it now. Can you try with it. If still there's an error check if your list actually receives any data – ljk Mar 05 '20 at 19:59
  • sir I don't get no errors the images are just not displaying at all – Supercoder54551 Mar 05 '20 at 20:14