0

i was trying to get this specific data from my database(kelas). What i'm doing here is to get this kelas value and store it to String kelas in getList() and use it in IF to check if it have value of 10 or 11, let say String kelas in getList() has value of 10, if it true then i'm gonna store some data in recycleview(databaseReference = FirebaseDatabase.getInstance().getReference("DaftarMapel10");), but it's not working, i tried to change the code but still got java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference.

As you can see in the comments, @Yegorft said that String kelas = snapshot.getValue(String.class); This var is null. Make sure, that it is present in your database. But i don't understand it, i tried to use constructor but same result.

UPDATE: so after read this answer from this post, no result. So here's what go wrong, my path in database didn't the equal to user id(.getUid), so that is why my String kelas is null or throw NullPointerException, it didn't get the value of kelas in database because the path is wrong or not equal to .getUid(). So whoever get the same problem like me, pls check the path in your database and the code, check the value.

Here is my Fragment:

public class NilaiFragment extends Fragment {

FirebaseAuth firebaseAuth;
String userID;
DatabaseReference databaseReference;
private NilaiViewModel nilaiViewModel;
DatabaseReference matchData;
FirebaseFirestore firebaseFirestore;
private List<NilaiHelperClass> list;
NilaiHelperAdapter nilaiHelperAdapter;
RecyclerView nilaiRecyclerView;
DatabaseReference rootRef;
DatabaseReference yourRef;
String userid;


public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    nilaiViewModel = new ViewModelProvider(this).get(NilaiViewModel.class);
    View root = inflater.inflate(R.layout.fragment_nilai, container, false);

    firebaseAuth = FirebaseAuth.getInstance();
    userID = firebaseAuth.getCurrentUser().getUid();
    userid = firebaseAuth.getCurrentUser().getUid();
    databaseReference = FirebaseDatabase.getInstance().getReference("DaftarMapel10");
    matchData = FirebaseDatabase.getInstance().getReference().child("AkunSiswa");

    nilaiRecyclerView = root.findViewById(R.id.rvNilaiKelas);
    nilaiRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    nilaiRecyclerView.setNestedScrollingEnabled(false);
    nilaiRecyclerView.setHasFixedSize(false);
    list = new ArrayList<>();


    getList();


    return root;
}

Here is where the error happened:

    private void getList(){

    FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
    firebaseDatabase.getReference().child("AkunSiswa").child(userid).child("kelas").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            String kelas = snapshot.getValue(String.class);
            if (kelas.equals("10")){
                databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        for(DataSnapshot dataSnapshot:snapshot.getChildren()){
                            NilaiHelperClass nilaiHelperClass = dataSnapshot.getValue(NilaiHelperClass.class);
                            list.add(nilaiHelperClass);
                        }
                        nilaiHelperAdapter = new NilaiHelperAdapter(list);
                        nilaiRecyclerView.setAdapter(nilaiHelperAdapter);
                    }

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

                    }
                });
            }
            if (kelas.equals("11")){
                nilaiRecyclerView.setVisibility(View.GONE);
            }
        }

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

        }
    });
}

this is the error:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
    at com.example.skripsi.ui.Nilai.NilaiFragment$1.onDataChange(NilaiFragment.java:125)
    at com.google.firebase.database.Query$1.onDataChange(Query.java:189)
    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:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:7948)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)

This is my database:

This is my database

Just in case if you wanna see my databaseReference(DaftarMapel10) looks like:

this is DaftarMapel10

KevinHB
  • 19
  • 2
  • 4
    Don't post code as image and along with code post your logcat. – Kunu Apr 05 '21 at 12:02
  • 1
    Lets start with: [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/q/3988788). This should let you locate code fragment in which you end up with calling `null.someMethod()` or `null.someField` or `null[index]`. After that main problem will be figuring out why was there `null` and preventing it. – Pshemo Apr 05 '21 at 12:16
  • 1
    String kelas = snapshot.getValue(String.class); This var is null. Make sure, that it is present in your database. – Yegorf Apr 05 '21 at 12:28
  • Please edit your question and add your database structure as a screenshot. Please respond with @AlexMamo – Alex Mamo Apr 05 '21 at 14:21
  • @AlexMamo thank you for responding to my question Alex, sorry for my mistake. this is my first time to post here. someone already edit my database as a image, it was a form of link. i did what u told me, edit my question. Pls take a look. – KevinHB Apr 06 '21 at 05:01

0 Answers0