I have this weird issue with Firebase. I am trying to retrieve the database in EditText so that i can update them but sometimes it retrieves them perfectly and sometimes suddenly gives a NullPointerException.
My Real Time Database:
Mobile->
Apple->
Iphone 13->
Display:"4K"
Processor:"A16"
Ram:"12GB"
Iphone 14->
Display:"4K"
Processor:"A16"
Ram:"12 GB"
One Plus->
One Plus 10->
Display:"2K"
Processor:"Snapdraggon895+"
Ram:"16 GB"
One Plus 11->
Display:"4K"
Processor:"Snapdraggon 950"
Ram:"16 GB"
My Update Activity:
<code>
package com.example.firebasepractice;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.snackbar.Snackbar;
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;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class UpdateDBActivity extends AppCompatActivity {
FirebaseDatabase firebaseDatabase;
EditText updateCompany, updateDevice, updateDisplay, updateProcessor, updateRam;
List<AppModelClass> myList;
AppModelClass model;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
updateCompany = findViewById(R.id.updateCompany);
updateDevice = findViewById(R.id.updateDevice);
updateDisplay = findViewById(R.id.updateDisplay);
updateProcessor = findViewById(R.id.updateProcessor);
updateRam = findViewById(R.id.updateRam);
model = new AppModelClass();
myList = new ArrayList<>();
}
public void getData(View view) {
String textCompany = updateCompany.getText().toString();
String textDevice = updateDevice.getText().toString();
DatabaseReference ref = FirebaseDatabase
.getInstance("https://fir-practice-4dc05-default-rtdb.asia-southeast1.firebasedatabase.app/")
.getReference()
.child("Mobile")
.child(textCompany)
.child(textDevice);
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
AppModelClass app = snapshot.getValue(AppModelClass.class);
updateProcessor.setText(app.getProcessor());
updateDisplay.setText(app.getDisplay());
updateRam.setText(app.getRam());
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
public void updateDB(View view) {
String textDisplay = updateDisplay.getText().toString();
String textProcessor = updateProcessor.getText().toString();
String textRam = updateRam.getText().toString();
String textCompany = updateCompany.getText().toString();
String textDevice = updateDevice.getText().toString();
if (textDisplay.isEmpty() || textDevice.isEmpty() || textCompany.isEmpty()
|| textRam.isEmpty() || textProcessor.isEmpty()) {
Toast.makeText(this, "Please fill all details", Toast.LENGTH_SHORT).show();
} else {
HashMap<String, Object> myMap = new HashMap<>();
myMap.put("Display", textDisplay);
myMap.put("Processor", textProcessor);
myMap.put("Ram", textRam);
FirebaseDatabase
.getInstance("https://fir-practice-4dc05-default-rtdb.asia-southeast1.firebasedatabase.app/")
.getReference("Mobile")
.child(textCompany)
.child(textDevice)
.setValue(myMap).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Toast.makeText(UpdateDBActivity.this, "Database Updated", Toast.LENGTH_SHORT).show();
updateCompany.setText("");
updateDevice.setText("");
updateRam.setText("");
updateDisplay.setText("");
updateProcessor.setText("");
}
});
}
}
}
</code>
In the getData() method i try to retrieve the JSON through my AppModelClass (Model class/POJO Class) and sometimes i am successfully retrieving and sometimes i get a NullPointerException on my AppModelClass object (app) in onDataChanged() method. What am i doing wrong ?