package com.tp468.dell.automatedfishpondmonitoringsystem;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class MainActivity extends AppCompatActivity {
TextView Temperature;
DatabaseReference dRef;
String status;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Temperature = (TextView)findViewById(R.id.textView);
dRef= FirebaseDatabase.getInstance().getReference();
dRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
status=dataSnapshot.child("Temperature").getValue().toString();
Temperature.setText(status);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
Asked
Active
Viewed 321 times
0

Andrew Thompson
- 168,117
- 40
- 217
- 433

Tushar Chaudhari
- 13
- 4
-
You need to check dataSnapshot.child("Temperature").getValue() if this expression returns a null or not.. just put up a null check in if condition before using toString on it. – Manoj Vadehra Jan 21 '19 at 16:43
-
how to resolve this error?? – Tushar Chaudhari Jan 21 '19 at 16:43
-
@TusharChaudhari by checking if it's null or not – Coderino Javarino Jan 21 '19 at 16:44
-
pls tell me in brief.. – Tushar Chaudhari Jan 21 '19 at 16:45
-
how i can put if condition please tell me? – Tushar Chaudhari Jan 21 '19 at 16:47
-
It is not an error, it is a warning generated by your IDE, because it was able to infer that there are codepaths where it might be null. – Mark Rotteveel Jan 21 '19 at 17:52
-
It will showing another warning as Casting 'findViewById(R.id.textView) to 'TextView' is redundant ..Can anyone help me to resolve this warning ? – Tushar Chaudhari Jan 23 '19 at 02:03
2 Answers
0
Try this: Use a null check first and then invoke a toString();
Object obj = dataSnapshot.child("Temperature").getValue();
if(obj!=null)
status = obj.toString();
//handle else condition too

Manoj Vadehra
- 836
- 4
- 17
0
Simply use the hasChild()
method as a check:
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild("Temperature")) {
status = dataSnapshot.child("Temperature").getValue().toString();
Temperature.setText(status);
}
}

Wills
- 61
- 4