java.lang.Object java.util.List.get(int)' on a null object reference



我必须从一个api获取信息,然而,并不是所有的数据都会到来,例如,有一些pokemon;技能;而其他人则不然。我认为我的Null Pointer Exception错误是存在的,但我不知道该怎么解决它,因为当我点击pokemon查看其详细信息时,会导致应用程序崩溃,正是因为它没有"能力;

日志错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pokedex, PID: 12090
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object 
java.util.List.get(int)' on a null object reference
at com.example.pokedex.ui.detalhesPokemons.DetalhesPokemonsFragment$configDetalhes$1
.onChanged(DetalhesPokem 
onsFragment.kt:45)
at com.example.pokedex.ui.detalhesPokemons.DetalhesPokemonsFragment$configDetalhes$1
.onChanged(DetalhesPokemonsFragment.kt:14)
at androidx.lifecycle.LiveData.considerNotify(LiveData.java:133)
at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:151)
at androidx.lifecycle.LiveData.setValue(LiveData.java:309)
at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
at com.example.pokedex.ui.detalhesPokemons.DetalhesPokemonsViewModel$getDetalhes$1
.invokeSuspend(DetalhesPok 
emonsViewModel.kt:20)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I/Process: Sending signal. PID: 12090 SIG: 9

类DetalhesPokemonsFragment(在其中它被拉入错误(

class DetalhesPokemonsFragment: Fragment() {
private val detalhesViewModel: DetalhesPokemonsViewModel by viewModels{
DetalhesPokemonsViewModelFactory((activity?.application as MyApplication).repository)
}
private val argumentos by navArgs<DetalhesPokemonsFragmentArgs>()
private val pokemon by lazy { argumentos.pokemon }
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(
R.layout.detalhes_pokemon,
container,
false
)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
configDetalhes()
}
private fun configDetalhes() {
detalhesViewModel.getDetalhes(pokemon.id)
detalhesViewModel.mResponse.observe(viewLifecycleOwner, {
if (it.isSuccessful) {
tv_detalhes_nome_pokemon.text = pokemon.nome
// line where the error appears is down here
if(pokemon.abilities[0].name == null){
pokemon.abilities[0].name =  "not abilities"
}else{
tv_detalhes_habilidades_pokemon.text =pokemon.abilities[0].name
}

tv_hp_detalhes_pokemon.text = pokemon.hp
}
})
}
}

无论出于何种原因,pokemon.abilities都为空。您正在尝试访问不存在的列表上的索引0。这就是为什么你会得到这个错误:

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object 
java.util.List.get(int)' on a null object reference

您不能在null上调用get(int)。在访问之前,您需要对其进行空检查:

pokemon.abilities?.get(0)
// or provide a fallback value for if you run into a null
pokemon.abilities?.get(0) ?: "sad pokemon"
// but you should use getOrNull so an empty list doesn't throw an exception
pokemon.abilities?.getOrNull(0) ?: "mysterious pokemon"

如果abilities是可以为null的,即List?,那么您的IDE应该对此发出警告。据猜测,您的API将返回List!,其中!意味着它来自Java,并且没有给出任何关于它是否可以为null。

但它肯定是可以为null的,因为你会得到一个null,所以也许你可以查看你正在使用的任何东西,并使用List?类型来帮助它,而API可能提供或不提供的任何其他属性也是如此,或者提供一些默认值,如emptyList()。无论你在用什么,你都必须研究一下它是如何工作的。

我通过将我正在进行的null检查从:"pokemon.abilities[0].name==null";至";pokemon.abilities==空";。正是这个错误让我没有正确地进行验证

相关内容

  • 没有找到相关文章

最新更新