Kawal Singh Kawal Singh - 1 year ago 196
Android Question

Firebase null pointer exception

I am getting null pointer exception in firebase I knows that there is not any value like that to which database reference I am giving but I wants that when the value is generated then it will be shown automatically...
Just wants to get that single value everytime it is created on the firebase but its giving me null pointer exception on the line String val =.....

Please help me ...

final DatabaseReference dtt = database.getReference("/trial/trials/");
dtt.addValueEventListener(new ValueEventListener() {

public void onDataChange(DataSnapshot dataSnapshot) {
String val= dataSnapshot.child("myvalue").getValue().toString();
Log.e("not null------","---------------");

public void onCancelled(DatabaseError databaseError) {


Please help me.....Please ..

Answer Source

You are using getValue() in wrong way. Should be

String val= dataSnapshot.child("myvalue").getValue(String.class);

To check, if value exists you can convert dataSnapshot to Hashmap, like this

HashMap<String, Object> hashmap = (HashMap) dataSnapshot.getValue();
String val= hashmap.get("myvalue");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download