如何在Java中的Equals函数上解决“ java.lang.NullPointerException:'?

问题描述 投票:0回答:1
@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)'

有人可以找到解决此错误的解决方案吗?我尝试了许多选项,但这些选项无法正常工作。

@@覆盖受保护的void onStart(){super.onStart(); databaseVaccine.addValueEventListener(new ValueEventListener(){@Override public void onDataChange(@ ...

java firebase nullpointerexception equals
1个回答
0
投票
将此IF更改为if(vaccine.CattleVaccineID!= null &&疫苗.CattleVaccineID.equals(k))
© www.soinside.com 2019 - 2024. All rights reserved.