Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Android Studio : Attempt to invoke virtual method 'Post.getPostimage()' on a null object reference

 private void getPostImage(final ImageView imageView, String postid){
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("posts").child(postid);
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Post post = dataSnapshot.getValue(Post.class);
              Glide.with(mContext.getApplicationContext()).load(post.getPostimage()).into(imageView);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

I’m getting an error that points to this line:

Glide.with(mContext.getApplicationContext()).load(post.getPostimage()).into(imageView);

Here is the error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.taimoorsikander.myapplication, PID: 20194
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.taimoorsikander.myapplication.Model.Post.getPostimage()' on a null object reference
        at com.taimoorsikander.myapplication.Adapter.NotificationAdapter$3.onDataChange(NotificationAdapter.java:177)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
        at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8633)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

I tried debugging the app to locate the null value, but everything seemed fine. There is no null value in this line. How can I fix this issue?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Value of Post in your NotificationAdapter is null

please Change

Glide.with(mContext.getApplicationContext()).load(post.getPostimage()).into(imageView);

to

if(post != null) {
    Glide.with(mContext.getApplicationContext()).load(post.getPostimage()).into(imageView);   
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading