I'm developing an app with Firebase that retrieves data realtime. I have 2 devices, and connected to Firebase, when first device is added some data to Firebase, the second device's job is to grab those data. But its always null and crashes first time it catches, and then if I reopen the app, its fine. Can anybody help me?
Here's my code:
mDB= FirebaseDatabase.getInstance().getReference();
mListItemRef = mDB.child("university/"+studencode);
mListItemRef.addChildEventListener(new ChildEventListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public void onChildAdded(com.google.firebase.database.DataSnapshot dataSnapshot, String s) {
FirebaseVariable university = dataSnapshot.getValue(FirebaseVariable.class);
if (university.getStatus().equals("0")){
createNotification();
Intent registerIntent = new Intent(MainActivityPerawat.this, Activity_Receiver.class);
registerIntent.putExtra("data",university.getData);
MainActivityPerawat.this.startActivity(registerIntent);
Log.e("HAHA ",university.getData() );
}
}
});
but its null when its retrive realtime for the first time.
edit : this my error
05-30 21:14:20.249 27974-27974/com.sinergiz
E/UncaughtException: java.lang.NullPointerException:
Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)'
on a null object reference
and i try to simple log
05-30 21:20:01.315 28948-28948/com.sinergiz E/HAHA: null
edited part 2:
this is my insert data
Firebase mRefstatus = mRef.child("status");
mRefstatus.setValue("0");
and when i reopen the apps. its fine ,this my on
this is solved by inset data with the child at the same time, heres example code
Map<String, String> map2 = new HashMap<String, String>();
map2.put("data1", data1);
map2.put("data2", data2);
mRef = new Firebase("your firebase url);
mRef.setValue(map2);
have a good days !