如何解决 Java 中 Equals 函数的“ java.lang.NullPointerException: '?
How to solve " java.lang.NullPointerException: ' on Equals function in Java?
@Override
protected void onStart() {
super.onStart();
databaseVaccine.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String k;
vaccineList.clear();
for(
DataSnapshot vaccineSnapshot :dataSnapshot.getChildren())
{
//getting farm
Vaccine vaccine = vaccineSnapshot.getValue(Vaccine.class);
k= getSfuff();
if (vaccine.CattleVaccineID.equals(k)) {
vaccineList.add(vaccine);
}
//adding farm to the list
}
VaccineList adapters = new VaccineList(ViewVaccination.this, vaccineList);
ListViewVaccine.setAdapter(adapters);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
public String getSfuff() {
Bundle bundle = getIntent().getExtras();
String stuff = bundle.getString("cattleID");
return stuff;
}
它在
行显示以下错误
"if (vaccine.CattleVaccineID.equals(k)) "
java.lang.NullPointerException: 尝试在空对象引用
上调用虚拟方法'boolean java.lang.String.equals(java.lang.Object)'
有人可以找到解决此错误的方法吗?我尝试了很多选项,但都无法正常工作。
将此 if
更改为
if (vaccine.CattleVaccineID != null && vaccine.CattleVaccineID.equals(k))
@Override
protected void onStart() {
super.onStart();
databaseVaccine.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String k;
vaccineList.clear();
for(
DataSnapshot vaccineSnapshot :dataSnapshot.getChildren())
{
//getting farm
Vaccine vaccine = vaccineSnapshot.getValue(Vaccine.class);
k= getSfuff();
if (vaccine.CattleVaccineID.equals(k)) {
vaccineList.add(vaccine);
}
//adding farm to the list
}
VaccineList adapters = new VaccineList(ViewVaccination.this, vaccineList);
ListViewVaccine.setAdapter(adapters);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
public String getSfuff() {
Bundle bundle = getIntent().getExtras();
String stuff = bundle.getString("cattleID");
return stuff;
}
它在
行显示以下错误"if (vaccine.CattleVaccineID.equals(k)) "
java.lang.NullPointerException: 尝试在空对象引用
上调用虚拟方法'boolean java.lang.String.equals(java.lang.Object)'有人可以找到解决此错误的方法吗?我尝试了很多选项,但都无法正常工作。
将此 if
更改为
if (vaccine.CattleVaccineID != null && vaccine.CattleVaccineID.equals(k))