-1

My Firebase database looks like above enter image description here I'm trying to retrieve the value of 'a' for 'alind' and similarly for another child node 'kartik' I'm using Android Studio. but i receive this 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.minorproject.facultyfreeslots.matchslots(facultyfreeslots.java:119)
        at com.example.minorproject.facultyfreeslots$1.onClick(facultyfreeslots.java:48)
        at android.view.View.performClick(View.java:6614)

I've tried setting different paths to access this using Datasnapshot.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_facultyfreeslots);
        t1=findViewById(R.id.textView50);
        name=findViewById(R.id.editText4);
        name2=findViewById(R.id.editText5);
        b1=findViewById(R.id.button9);
        day=findViewById(R.id.spinner18);
        time=findViewById(R.id.spinner19);
        FirebaseApp.initializeApp(this);
        d1= FirebaseDatabase.getInstance().getReference("teacher");
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                matchslots();
            }
        });
    }
void matchslots()
    {  // getdata();
        name11=name.getText().toString();
        name12=name.getText().toString();
        daystring=day.getSelectedItem().toString().trim();
        timestring=time.getSelectedItem().toString().trim();
        d1.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                dvalue1=dataSnapshot.child(name11).child(daystring).child(timestring1).getValue().toString().trim();
                dvalue2=dataSnapshot.child(name12).child(daystring).child(timestring1).getValue().toString().trim();
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.d(TAG, "Failed to read value.");
            }
        });
 //line119- 
  if(dvalue1.equals("no class"))
        {
            a=0;
        }

        else {
            a=1;
        }
        if(dvalue2.equals("no class"))
        {
            b=0;
        }
        else
        {
            b=1;

I want to get the value of a or b or c. nam11=alind or any othername, daystring="monday"/"tuesday" etc
timestring="a"/"b"/"c" etc



Kartik Singh
  • 43
  • 1
  • 6

1 Answers1

2

Since onDataChange() is asynchronous, then you have to do the following:

d1.addValueEventListener(new ValueEventListener() {
 @Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
   dvalue1=dataSnapshot.child(name11).child(daystring).child(timestring1).getValue().toString().trim();
   dvalue2=dataSnapshot.child(name12).child(daystring).child(timestring1).getValue().toString().trim();
   if(dvalue1.equals("no class"))
    {
        a=0;
    }

    else 
    {
        a=1;
    }
    if(dvalue2.equals("no class"))
    {
        b=0;
    }
    else
    {
        b=1;
     }
   }
 @Override
public void onCancelled(@NonNull DatabaseError databaseError) {
 Log.d(TAG, "Failed to read value.");
  }
});

The code after your listener is getting run before retrieving all the data, therefore you get the error that dvalue1 is null.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134