This is a simple sample of a crud function with Firebase via Android Studio. I found several obstacles in the update process, along with errors that appeared when updating.
java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
Is there a simple solution to overcome this? Here is the complete code for the update activity.
import static android.text.TextUtils.isEmpty;
import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
//import com.bumptech.glide.Glide;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.text.SimpleDateFormat;
public class UpdateLaundry extends AppCompatActivity {
//deklarasi variable
private EditText namaBaru, tempatBaru;
private Spinner jenisBaru;
private RadioButton ktgBaru, new_Rb1, new_Rb2, new_Rb3;
private Button update;
private DatabaseReference database;
private RadioGroup rgkategoriBaru;
private String cekNama, cekTempat, cekJenis, cekKategori;
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_updatelaundry);
namaBaru = findViewById(R.id.new_nama);
tempatBaru = findViewById(R.id.new_lokasi);
jenisBaru = findViewById(R.id.newspinner_jenis);
update = findViewById(R.id.btn_update);
new_Rb1 = findViewById(R.id.new_rb1);
new_Rb2 = findViewById(R.id.new_rb2);
new_Rb3 = findViewById(R.id.new_rb3);
rgkategoriBaru = findViewById(R.id.new_rg_ktg);
final String getKategori = getIntent().getExtras().getString("dataKategori");
if (getKategori.equals("Cuci")) {
new_Rb1.setChecked(true);
} else if (getKategori.equals("Setrika")) {
new_Rb2.setChecked(true);
} else if (getKategori.equals("Cuci dan Setrika")) {
new_Rb3.setChecked(true);
}
database= FirebaseDatabase.getInstance().getReference();
getData();
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pilihKtgBaru = rgkategoriBaru.getCheckedRadioButtonId();
ktgBaru = findViewById(pilihKtgBaru);
cekNama = namaBaru.getText().toString();
cekTempat = tempatBaru.getText().toString();
cekJenis = jenisBaru.getSelectedItem().toString();
//mengubah data databse menggunakan data yang baru di inputkan
if (isEmpty(cekNama) || isEmpty(cekTempat)) {
Toast.makeText(UpdateLaundry.this, "Data tidak boleh kosong", Toast.LENGTH_SHORT).show();
} else {
/*Processing Update Data
Reload setter recomended to get new data from user*/
Laundry setLaundry = new Laundry();
setLaundry.setNama(namaBaru.getText().toString());
setLaundry.setJenis(jenisBaru.getSelectedItem().toString());
setLaundry.setTempat(tempatBaru.getText().toString());
setLaundry.setKategori(ktgBaru.getText().toString());
updateLaundry(setLaundry);
}
}
});
}
private boolean isEmpty(String s){
return TextUtils.isEmpty(s);
}
//mengambil data dari database ke form update
private void getData() {
final String getNama = getIntent().getExtras().getString("dataNama");
final String getTempat = getIntent().getExtras().getString("dataTempat");
final String getKategori = getIntent().getExtras().getString("dataKategori");
final String getJenis = getIntent().getExtras().getString("dataJenis");
namaBaru.setText(getNama);
tempatBaru.setText(getTempat);
}
private void updateLaundry(Laundry ldry) {
String getKey = getIntent().getExtras().getString("getPrimaryKey");
database.child("Admin")
.child("Laundry")
.child(getKey)
.setValue(ldry)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
namaBaru.setText("");
tempatBaru.setText("");
Toast.makeText(UpdateLaundry.this, "Data Berhasil diubah", Toast.LENGTH_SHORT).show();
finish();
}
});
}