gpt4 book ai didi

java.lang.NullPointerException : Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)'

转载 作者:行者123 更新时间:2023-12-02 04:05:09 28 4
gpt4 key购买 nike

我尝试将 WordPress 转换为移动应用程序,但这里出现错误 response.body().get(i).getLinks().getWpFeaturedmedia().get(0).getHref()) );

请帮我解决这个问题:

call.enqueue(new Callback<List<Kesehatan>>() {
@Override
public void onResponse(Call<List<Kesehatan>> call, Response<List<Kesehatan>> response) {
Log.e("kesehatan fragment", " response " + response.body());

mListPost = response.body();
progressBar.setVisibility(View.GONE);

for (int i = 0; i < response.body().size(); i++) {

String temptdetails = response.body().get(i).getExcerpt().getRendered().toString();
temptdetails = temptdetails.replace("<p>", "");
temptdetails = temptdetails.replace("</p>", "");
temptdetails = temptdetails.replace("[&hellip;]", "");

list.add(new Model(Model.IMAGE_TYPE, response.body().get(i).getTitle().getRendered(),
temptdetails,
response.body().get(i).getLinks().getWpFeaturedmedia().get(0).getHref()));

Log.e("main ", " title " + response.body().get(i).getTitle().getRendered() + " " +
response.body().get(i).getId());

}

adapter.notifyDataSetChanged();

}

错误:

/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dimasgs.bvet, PID: 11710
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
at com.example.dimasgs.bvet.Fragments.KesehatanFragment$1.onResponse(KesehatanFragment.java:92)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7073)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)

最佳答案

问题是因为您使用以下代码读取响应流两次:

Log.e("kesehatan fragment", " response " + response.body());

mListPost = response.body();

您需要将 response.body() 值存储为字符串。像这样的事情:

String responseBody = response.body();
Log.e("kesehatan fragment", " response " + responseBody);
mListPost = responseBody;

有关详细信息,请参阅此问题:https://github.com/square/okhttp/issues/1240

关于java.lang.NullPointerException : Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56716889/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com