I am currently working on a project to push data from a firebase real time database into an android application using android studio. I have already been successfully able to display data from other certain parts of the database but I am having trouble displaying data from a portion of the database labeled "TH" and I am not sure why. Here is my code for reference:
package com.example.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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 mcu2temphumid extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mcu2temphumid);
TextView temp1 = findViewById(R.id.temp);
TextView temp2 = findViewById(R.id.temp4);
TextView temp3 = findViewById(R.id.temp5);
// ethy4 = findViewById(R.id.ethy4);
Button buttonreferesh = (Button) findViewById(R.id.refreshmcu1temp2);
Button buttonback = (Button) findViewById(R.id.backmcu1temp2);
DatabaseReference rootDatabaseref = FirebaseDatabase.getInstance().getReference().child("MCU 2").child("TH").child("Sensor 1");//.child("TH").child("Sensor 1");
DatabaseReference rootDatabaseref2 = FirebaseDatabase.getInstance().getReference().child("MCU 2").child("TH").child("Sensor 1");
DatabaseReference rootDatabaseref3 = FirebaseDatabase.getInstance().getReference().child("MCU 2").child("TH").child("Sensor 1");
//DatabaseReference rootDatabaseref4 = FirebaseDatabase.getInstance().getReference().child("MCU 1").child("Ethylene").child("Sensor 4");
buttonback.setOnClickListener(new View.OnClickListener() { //this section will allow the button to perform the method call when the button is pressed
@Override
public void onClick(View view) {
openActivitymain();
}
});
buttonreferesh.setOnClickListener(new View.OnClickListener() { //this section will allow the button to perform the method call when the button is pressed
@Override
public void onClick(View view) {
openActivityrefresh();
}
});
rootDatabaseref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String data = snapshot.getValue().toString();
Double z = Double.parseDouble(data);
temp1.setText(String.valueOf(z));
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
rootDatabaseref2.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String data = snapshot.getValue().toString();
Double z = Double.parseDouble(data);
temp2.setText(String.valueOf(z));
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
rootDatabaseref3.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String data = snapshot.getValue().toString();
Double z = Double.parseDouble(data);
temp3.setText(String.valueOf(z));
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
public void openActivityrefresh(){
Intent intent = new Intent(this, mcu2temphumid.class); //causes the subordinate activity file to be opened, redirects to new layout
startActivity(intent);
}
public void openActivitymain(){
Intent intent = new Intent(this, MCU2.class); //causes the subordinate activity file to be opened, redirects to new layout
startActivity(intent);
}
}
Here is a snippet of my database in firebase: Database
The following is the error I am receiving:
2022-09-19 08:53:49.900 18098-18098/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 18098
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.myapplication.mcu2temphumid$3.onDataChange(mcu2temphumid.java:51)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6747)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:449)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
As mentioned before, I have used nearly identical code to successfully display data from other portions of the database, so I am not sure what the problem is. From my understanding getting a null object reference would indicate that there isnt any value being taken in for String data, but I clearly gave values to the corresponding entries in my database. Does anyone know what may be causing this?