2

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 !

AL.
  • 36,815
  • 10
  • 142
  • 281
themasmul
  • 125
  • 1
  • 10
  • Please edit your question to include the stack trace of the crash. – Frank van Puffelen May 30 '17 at 14:08
  • @FrankvanPuffelen i just edited my question – themasmul May 30 '17 at 14:15
  • It seems like the first child doesn't have a status. But it's hard to be certain from the information you shared. There's also going to be a line number with that crash. Find that line in your code and it will tell you where you should check for `null`. See https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Frank van Puffelen May 30 '17 at 14:43
  • Could you include how you are adding the data in the question. The fact that it throws the NullPointer initially but not when reopened suggests perhaps all the data isn't there the first time `onChildAdded` is called. – Lewis McGeary May 30 '17 at 15:34
  • @FrankvanPuffelen the database is fine, all the data is there, but somehow i cant get the data. its say null – themasmul May 30 '17 at 15:42
  • @LewisMcGeary i will update it, please stay tune, – themasmul May 30 '17 at 15:43
  • You only write one piece of data? just status? – Lewis McGeary May 30 '17 at 15:46
  • @LewisMcGeary , no its not my all code. i just make it simple in this questin. but i guarantee that the data all is stored fine in firebase. but i cant retrieve it. i can send all my code if you wan help me further. – themasmul May 30 '17 at 15:52

1 Answers1

1

This seems like a case of values being written separately rather than as one 'object' (though that is sometimes the correct thing to do).

So if I add a fictional data point to the one included in the question we might have:

DatabaseReference mRefSomeProperty = mRef.child("someProperty");
DatabaseReference mRefstatus = mRef.child("status");

mRefSomeProperty.setValue("someValue");
mRefstatus.setValue("0");

These two setValue calls are separate write operations, if you have a listener it won't wait for both values to be written, the first setValue for "someProperty" will trigger the listener, and if your listener tries to do something with "status" it will cause problems, status isn't there yet.

You can try using your data class when setting the value, in a similar way as you use it when getting the value back. So our code could look like:

FirebaseVariable university = new FirebaseVariable("someValue", "0");
mRef.setValue(university);

This assumes that FirebaseVariable has those two properties and a constructor to take them, could instead use setStatus or other way to create the object.

This should now be treated as one write operation so everything will be there when your listener is triggered rather than being triggered earlier when the first value has been written.

Lewis McGeary
  • 7,692
  • 2
  • 40
  • 46