-1

I don't know what make problem that show follower follow list, it was working but after few days later (maybe there is possbility i touch something...) it didn't working

what makes it error NullPointerException?

This is my Error

2019-06-17 11:53:31.158 8298-8298/com.example.blogapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.blogapp, PID: 8298
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at com.example.together.Activities.FollowersActivity$5.onDataChange(FollowersActivity.java:157)
        at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@17.0.0:184)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@17.0.0:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@17.0.0:63)
        at com.google.firebase.database.core.view.

Nothing showing redline ... just null point

This is my btn that i clicked*


 followers.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), FollowersActivity.class);
                intent.putExtra("id", profileid);
                intent.putExtra("title","followers");
                startActivity(intent);
            }
        });





        following.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), FollowersActivity.class);
                intent.putExtra("id", profileid);
                intent.putExtra("title","following");
                startActivity(intent);
            }
        });



This is my FollowersActivity

package com.example.together.Activities;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.example.together.Adapter.UserAdapter;
import com.example.together.Model.User;
import com.example.together.R;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

public class FollowersActivity extends AppCompatActivity {

    String id;
    String title;

    List<String> idList;

    RecyclerView recyclerView;
    UserAdapter userAdapter;
    List<User> userList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_followers);

        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        title = intent.getStringExtra("title");

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });



        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        userList = new ArrayList<>();
        userAdapter = new UserAdapter(this, userList, false);
        recyclerView.setAdapter(userAdapter);

        idList = new ArrayList<>();

        switch (title){
            case "likes":
                getLikes();
                break;
            case "following":
                getFollowing();
                break;
            case "followers":
                getFollowers();
                break;
        }

    }

    private void getLikes() {

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Likes")
                .child(id);
        reference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                idList.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                    idList.add(snapshot.getKey());
                }
            }

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

            }
        });

    }


    private void getFollowing(){

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Follow")
                .child(id).child("following");
        reference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                idList.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                    idList.add(snapshot.getKey());
                }
                showUsers();
            }

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

            }
        });

    }


    private void getFollowers(){
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Follow")
                .child(id).child("followers");
        reference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                idList.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                    idList.add(snapshot.getKey());
                }
                showUsers();
            }

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

            }
        });
    }


    private void showUsers(){
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
        reference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                userList.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                    User user = snapshot.getValue(User.class);
                    for (String id : idList){
                        if (user.getId().equals(id)){
                            userList.add(user);
                        }
                    }
                }

                userAdapter.notifyDataSetChanged();
            }

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

            }
        });
    }



}

ru hee
  • 103
  • 1
  • 1
  • 7

2 Answers2

0

The user object is null and you are trying to retrieve data from null object

reference.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    userList.clear();
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                        User user = snapshot.getValue(User.class);
// check user exists or not.. here user gets null
                         if(user!=null){
                        for (String id : idList){
                            if (user.getId().equals(id)){
                                userList.add(user);
                            }
                        }}
                    }
Sultan Mahmud
  • 1,245
  • 8
  • 18
0

Check a profileid on first activity.

intent.putExtra("id", profileid);

and make sure print a log here is you get a value of profileid

Gaurav
  • 171
  • 1
  • 13
  • i checked but it show to me null.... Why suddenly this happend.. – ru hee Jun 17 '19 at 05:03
  • try some static string instead of profileid and check to pass this value and another activity check value is getting. – Gaurav Jun 17 '19 at 05:23
  • i saw the log the problem is one of my followers can't find but why this happening? that's why null exception to show in my log – ru hee Jun 17 '19 at 05:50
  • static string is getting in another activity? – Gaurav Jun 17 '19 at 05:56
  • i write log Log.e("idList",idList+""); for (String id : idList){ if (user.getId().equals(id)){ Log.e("compare out id",user.getId()); Log.e("arrayList id",id+""); userList.add(user); } } } – ru hee Jun 17 '19 at 06:05
  • compare out id & arraylist id log is printed in logcat and also check idList.size() – Gaurav Jun 17 '19 at 06:10
  • and the problem is from Log "arrayList" this not miss out one id but i don't understand why he missing just one – ru hee Jun 17 '19 at 06:10
  • check size missin elemet is in the arraylist for(int i=0;i – Gaurav Jun 17 '19 at 06:12
  • System.out.println("-------- number"+idList.size()); Log.e("check","id--> "+id); The result shwoing idList.size --> 14 second Log.e("check",id) showing 13 one missing ... – ru hee Jun 17 '19 at 06:16
  • can you share a log? both size and elements – Gaurav Jun 17 '19 at 06:20
  • 2019-06-17 15:15:00.778 14265-14265/com.example.blogapp I/System.out: -------- size ----- 14 2019-06-17 15:15:00.778 14265-14265/com.example.blogapp E/check: ㅅㅅㅅㅅㅅ oT1sKEE9ILZjNn2B5O4Yf7gjNqV2 2019-06-17 15:15:00.778 14265-14265/com.example.blogapp W/ClassMapper: No setter/field for -LgUWUiyQmk0fwrqwbX4 found on class com.example.together.Model.User – ru hee Jun 17 '19 at 06:26
  • 2019-06-17 15:15:00.779 14265-14265/com.example.blogapp E/idList: [2okCgbdTqlVgK6oDqPiYPwyM0kD3, 2zUL5Bv7CcPnBENXuWZrr1bxbgs1, 6YZdQrf5RycKO1kwvze0vCXL8V83, BCka8CPfqhfyWZlhgVAwJXDA8fi1, BcNxFCVZZrgxC0pp48G5LQqEP2B2, DAiGCMPqcyOdjIAU7RvHWC0xfbG2, Meqo3PIqWrVv66kmGtVDWXknHbh1, NTq9UMZTBefE5Ze2lZdc9hGGhtZ2, P8uBLb9Ge5MPRbcgrOkl1xJMymI3, Sjzjl1pD2PQBoCcc1x0Ji4gaFkI3, VxLgeLvRV9RZO8yg5MfYM2CrsfJ3, lPKdx41KR2R3PsZh62ZM3QuyuS93, oT1sKEE9ILZjNn2B5O4Yf7gjNqV2, y8R7pOu6ALQ95UlVAA2GNwLm2Ex1] 2019-06-17 15:15:00.779 14265-14265/com.example.blogapp D/AndroidRuntime: Shutting down VM – ru hee Jun 17 '19 at 06:26
  • 2019-06-17 15:15:00.780 14265-14265/com.example.blogapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.blogapp, PID: 14265 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference at com.example.together.Activities.FollowersActivity$5.onDataChange(FollowersActivity.java:162) – ru hee Jun 17 '19 at 06:26
  • This is my App the follower and follow list not showing https://github.com/dodose/Together – ru hee Jun 17 '19 at 06:26
  • there is a size is 14 and the idList there is also 14 elements – Gaurav Jun 17 '19 at 06:33
  • Yes that's right but elements showing just 13... – ru hee Jun 17 '19 at 06:43
  • that is why i can't understand – ru hee Jun 17 '19 at 06:43
  • Log.e("idList size",""+idList.size()); for (int i=0;i – Gaurav Jun 17 '19 at 06:56
  • replace with your code and check idList no in logcat – Gaurav Jun 17 '19 at 06:56
  • 2019-06-17 16:06:47.278 15526-15526/com.example.blogapp E/idList no: 1 2019-06-17 16:06:47.278 15526-15526/com.example.blogapp E/idList id: fm6XldWYbOdVQYOuiTVLTRouN573 2019-06-17 16:06:47.278 15526-15526/com.example.blogapp E/idList no: 2 2019-06-17 16:06:47.278 15526-15526/com.example.blogapp E/idList id: fm6XldWYbOdVQYOuiTVLTRouN573 – ru hee Jun 17 '19 at 07:09
  • .... ~ 2019-06-17 16:06:47.280 15526-15526/com.example.blogapp E/idList no: 13 2019-06-17 16:06:47.280 15526-15526/com.example.blogapp E/idList id: fm6XldWYbOdVQYOuiTVLTRouN573 2019-06-17 16:06:47.280 15526-15526/com.example.blogapp E/idList no: 14 2019-06-17 16:06:47.280 15526-15526/com.example.blogapp E/idList id: fm6XldWYbOdVQYOuiTVLTRouN573 2019-06-17 16:06:47.280 15526-15526/com.example.blogapp W/ClassMapper: No setter/field for -LgUWUiyQmk0fwrqwbX4 found on class com.example.together.Model.User 2019-06-17 16:06:47.280 15526-15526/com.example.blogapp E/idList no: 1 – ru hee Jun 17 '19 at 07:10
  • 2019-06-17 16:06:47.280 15526-15526/com.example.blogapp E/idList id: fm6XldWYbOdVQYOuiTVLTRouN573 2019-06-17 16:06:47.280 15526-15526/com.example.blogapp D/AndroidRuntime: Shutting down VM --------- beginning of crash 2019-06-17 16:06:47.284 15526-15526/com.example.blogapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.blogapp, PID: 15526 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference – ru hee Jun 17 '19 at 07:11