尝试在空对象引用上调用虚拟方法 'int java.util.ArrayList.size()' 的操作。

huangapple go评论64阅读模式
英文:

Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference android studio

问题

以下是翻译好的内容:

我正在尝试使用Android Studio构建一个应用程序。我正在按照一个教程进行操作,但问题是,那个人在暂停屏幕以启动模拟器时,当他再次播放时,有一段代码丢失了。我不确定他确切做了什么,但我尝试了我所有可以找到的选项。

我遇到了这个错误:
java.lang.NullPointerException: 尝试调用空对象引用上的 'int java.util.ArrayList.size()' 的虚拟方法

代码:

public void onResponse(Call<Users> call, Response<Users> response) {
    if (response.isSuccessful()) {
        mediaObjectList = (ArrayList<MediaObject>) response.body().getAllPosts();
        recyclerview.setMediaObjects(mediaObjectList);
        
        VideoPlayerRecyclerAdapter adapter = new VideoPlayerRecyclerAdapter(mediaObjectList, initGlide());
        recyclerview.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        recyclerview.setKeepScreenOn(true);
        recyclerview.smoothScrollToPosition(mediaObjectList.size()+1);
    
    } else {
        Toast.makeText(HomeActivity.this, "网络错误。", Toast.LENGTH_SHORT).show();
    }
}

我在这一行代码上遇到了错误:

recyclerview.smoothScrollToPosition(mediaObjectList.size()+1);

英文:

I am trying to build an app with android studio. I am following a tutorial but the thing is, the guy paused the screen to start the emulator and when he played it again a piece of code was missing. I don't know for sure what he did but I tried all of the options presented to me.

I am getting this error:
java.lang.NullPointerException: Attempt to invoke virtual method &#39;int java.util.ArrayList.size()&#39; on a null object reference

code:

public void onResponse(Call&lt;Users&gt; call, Response&lt;Users&gt; response) {
    if (response.isSuccessful()) {
        mediaObjectList = (ArrayList&lt;MediaObject&gt;) response.body().getAllPosts();
        recyclerview.setMediaObjects(mediaObjectList);
        
        VideoPlayerRecyclerAdapter adapter = new VideoPlayerRecyclerAdapter(mediaObjectList, initGlide());
        recyclerview.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        recyclerview.setKeepScreenOn(true);
        recyclerview.smoothScrollToPosition(mediaObjectList.size()+1);
    
    } else {
        Toast.makeText(HomeActivity.this, &quot;Network Error.&quot;, Toast.LENGTH_SHORT).show();
    }
}

I'm getting the error on this line:

recyclerview.smoothScrollToPosition(mediaObjectList.size()+1);

答案1

得分: 1

检查是否`getAllPost()`有值或为空

***正确方式***

    public void onResponse(Call<Users> call, Response<Users> response) {
        if (response.isSuccessful()) {
            mediaObjectList = (ArrayList<MediaObject>) response.body().getAllPosts();
            if (mediaObjectList != null) {
                recyclerview.setMediaObjects(mediaObjectList);
                VideoPlayerRecyclerAdapter adapter = new VideoPlayerRecyclerAdapter(mediaObjectList, initGlide());
                recyclerview.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                recyclerview.setKeepScreenOn(true);
                recyclerview.smoothScrollToPosition(mediaObjectList.size() + 1);
            } else {
                Toast.makeText(HomeActivity.this, "没有可用的帖子。", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(HomeActivity.this, "网络错误。", Toast.LENGTH_SHORT).show();
        }
    }
英文:

Check if getAllPost() has value or not

Correct Way:

public void onResponse(Call&lt;Users&gt; call, Response&lt;Users&gt; response) {
            if (response.isSuccessful()) {
                mediaObjectList = (ArrayList&lt;MediaObject&gt;) response . body ().getAllPosts();
                if (mediaObjectList != null) {
                    recyclerview.setMediaObjects(mediaObjectList);
                    VideoPlayerRecyclerAdapter adapter = new VideoPlayerRecyclerAdapter(mediaObjectList, initGlide());
                    recyclerview.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    recyclerview.setKeepScreenOn(true);
                    recyclerview.smoothScrollToPosition(mediaObjectList.size() + 1);
                } else {
                    Toast.makeText(HomeActivity.this, &quot;No Post Available.&quot;, Toast.LENGTH_SHORT).show();
                }
            }
            else {
                Toast.makeText(HomeActivity.this, &quot;Network Error.&quot;, Toast.LENGTH_SHORT).show();
            }
        }

huangapple
  • 本文由 发表于 2020年9月7日 02:24:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63767540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定