- 此问题在此处已有答案**:
What is a NullPointerException, and how do I fix it?(12个答案)
昨天关门了。
这是一个通过Android Studio使用Firebase的Crud函数的简单示例,我在更新过程中发现了几个障碍,以及更新时出现的错误。
java.lang.NullPointerException:无法为child()中的参数'pathString'传递null
有简单的解决方案来克服这个问题吗?下面是更新活动的完整代码。
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();
}
});
}
1条答案
按热度按时间weylhg0b1#
如果我是对的,那么
updateLaundry()
方法中的getKey
变量是可以为空的,因为您将intent键getPrimaryKey
的值设置为该变量。因此,无论何时您没有通过intent传递getPrimaryKey
的值,getKey
都将被设置为空,并且可能发生这种NPE。因此,要么在使用
getkey
之前对其进行空值检查,要么确保通过上一个Activity中的Intent传递密钥。