Ad

Retrieving Data Into Recyclerview

- 1 answer

Running the app gives null pointer exception

 Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference

so I assume my RecyclerViewer is not getting the data, not sure what is causing it.

Model Class - Images.java

package com.example.alber.undesiredapplication.model;


import com.google.firebase.database.Exclude;

public class Images {

private String image_url2;
private String building_id;
private String mKey;

public Images(String image_url2, String building_id) {
    this.image_url2 = image_url2;
    this.building_id = building_id;
}

public String getImage_url2() {
    return image_url2;
}

public String getBuilding_id() {
    return building_id;
}

public void setImage_url2(String image_url2) {
    this.image_url2 = image_url2;
}

public void setBuilding_id(String building_id) {
    this.building_id = building_id;
}


@Exclude
public String getKey() {
    return mKey;
}

@Exclude
public void setKey(String key) {
    mKey = key;
}
}

RecyclerViewAdapter

public class ImagesAdapter extends RecyclerView.Adapter<ImagesAdapter.ImageViewHolder> {

private Context mContext;
private List<Images> mUploads;


public ImagesAdapter(Context context, List<Images> uploads) {
    mContext = context;
    mUploads = uploads;
}

@Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(mContext).inflate(R.layout.layout_view_post, parent, false);
    return new ImageViewHolder(v);
}

@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
    Picasso.get().load(mUploads.get(position).getImage_url2()).fit().centerCrop().into(holder.imageView);
}


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

public class ImageViewHolder extends RecyclerView.ViewHolder{

    public ImageView imageView;
    LinearLayout view_container;

    public ImageViewHolder(View itemView) {
        super(itemView);

        imageView = itemView.findViewById(R.id.thumbnail);
        view_container = itemView.findViewById(R.id.container);
    }
}
}

Activity the RecyclerView is inside of

public class ProfileActivity2 extends AppCompatActivity implements OnMapReadyCallback {


private FloatingActionButton mfloating;
private String b_id;
private RecyclerView mRecyclerview;
private ImagesAdapter mAdapter;
private List<Images> mImages;
private FirebaseStorage mStorage;
private DatabaseReference mDatabaseRef;
private ValueEventListener mDBListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_profile2);

    mfloating = findViewById(R.id.floatingActionButton2);
    mRecyclerview = findViewById(R.id.recycler_view_profile);
    mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
    mAdapter = new ImagesAdapter(ProfileActivity2.this, mImages);
    mRecyclerview.setAdapter(mAdapter);
    mStorage = FirebaseStorage.getInstance();
    mDatabaseRef = 
    FirebaseDatabase.getInstance().getReference("Building_Images");
    mImages = new ArrayList<>();

    mDBListener = mDatabaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            mImages.clear();

            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Images upload = postSnapshot.getValue(Images.class);
                upload.setKey(postSnapshot.getKey());
                mImages.add(upload);

            }

            mAdapter.notifyDataSetChanged();

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(ProfileActivity2.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });



    mfloating.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String buildingId = b_id;

            Intent startIntent = new Intent(ProfileActivity2.this, ImagesUploadActivity.class);
            startIntent.putExtra("b_id", buildingId);
            startActivity(startIntent);
            finish();
        }
    });



   }

Here is what my database structure looks like

  "Building_Images" : {
"-Llry4s_9RKTfhEmurMh" : {
  "building_id" : "-LlON58gBZ-LVuEDtZ09",
  "image_url2" : "https://firebasestorage.googleapis.com/v0/b/undesired-project.appspot.com/o/Buildings%..."
},

What am I doing wrong? Is my database structure alright?

Ad

Answer

mRecyclerview = findViewById(R.id.recycler_view_profile);

findViewById is returning null in this line. Make sure a RecyclerView with id recycler_view_profile is present in your activity_profile2.xml layout file.

Ad
source: stackoverflow.com
Ad