为什么我会在这段代码中得到 nullPointerException?

我有一个活动,并且在单击方法的最后一行得到一个 nullPointerException


public class MainActivity extends AppCompatActivity {


List<Item> it;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);   

    }


    public void clicked(View view) {          

        getCompany(s);

        Integer.toString(it.size());  

    }


    public void getCompany(String name) {


        apiCall.setCallback(new ApiCall.NetworkCallback() {

            @Override

            public void onResponse(List<Item> items) {

                 it = items;

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

                    Log.d("Name", it.get(i).getTitle());

                }

            }

        });

    }

我删除了一些代码以保持简短,但问题是在 onResponse 方法中可以访问“it”列表,但在 clicked 方法中给出了 nullPointerException。


什么会导致这种情况以及如何存储要在 onResponse 方法之外使用的列表?


回首忆惘然
浏览 76回答 3
3回答

慕盖茨4494581

因为您尝试在初始化之前获取列表的大小。在 Integer.toString(it.size());因为它还没有初始化 getCompany(s); 是异步方法尝试删除 Integer.toString(it.size());因为在不使用和没有用处。公共类 MainActivity 扩展 AppCompatActivity {List<Item> it;@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp;}public void clicked(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; getCompany(s);}public void getCompany(String name) {&nbsp; &nbsp; apiCall.setCallback(new ApiCall.NetworkCallback() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(List<Item> items) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;it = items;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < items.size(); i++) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("Name", it.get(i).getTitle());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}

PIPIONE

我会直接为您提供一个可能的解决方案,它为您提供一个界面的快速示例public class MainActivity extends AppCompatActivity {&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; public void clicked(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; getCompany(s, new OnReceiveCompany() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onReceiveCompany(List<Item> it) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Integer.toString(it.size());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; public void getCompany(String name, final OnReceiveCompany callback) {&nbsp; &nbsp; &nbsp; &nbsp; apiCall.setCallback(new ApiCall.NetworkCallback() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(List<Item> items) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < items.size(); i++) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("Name", it.get(i).getTitle());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // make sure this run on UI thread if you need to perform ui operation. You can wrap it with new Hanler(Looper.getMainLooper).post(...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callback.onReceiveCompany(items);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; interface OnReceiveCompany {&nbsp; &nbsp; &nbsp; &nbsp; void onReceiveCompany(List<Item> it);&nbsp; &nbsp; }&nbsp;}为什么您的代码不起作用:&nbsp; &nbsp; public void clicked(View view) {&nbsp; &nbsp; &nbsp; &nbsp; // this method is gonna set "it" in the block of code in onResponse, which will happens after you call Integer.toString probably in a separate thread&nbsp; &nbsp; &nbsp; &nbsp; getCompany(s);&nbsp; &nbsp; &nbsp; &nbsp; Integer.toString(it.size());&nbsp;&nbsp;&nbsp; &nbsp; }

交互式爱情

当您尝试使用空变量(或尚未初始化的变量)时,会发生 NullPointerException。即使onResponse()从内部调用clicked(),您也必须记住这onResponse是在异步调用中。这仅仅意味着不能保证在调用onResponse之前完成执行。clicked()我建议添加Integer.toString(it.size())到您的onResponse()方法中。我希望这有帮助。不要再挣扎了 ;-) 快乐的编码!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java