Reputation: 65
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() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String val= dataSnapshot.child("myvalue").getValue().toString();
if(val!=null)
{
Log.e("not null------","---------------");
}else
{
Log.e("null------","---------------");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Please help me.....Please ..
Upvotes: 3
Views: 3092
Reputation: 401
final DatabaseReference dtt = database.getReference("/trial/trials/");
dtt.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String val= dataSnapshot.child("myvalue").getValue(String.class);
if(val!=null)
{
Log.e("not null------","---------------");
}else
{
Log.e("null------","---------------");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Upvotes: 7
Reputation: 11185
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");
Upvotes: 2