齐射未回复

时间:2019-06-09 19:25:24

标签: java android nullpointerexception android-volley

我正在创建多项选择测验,我正在尝试使用齐射从服务器检索数据,当我调用此方法时,我从服务器获得了响应,但该方法返回了null。

 protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            contactList = loadJsonArray();
            rb4.setText(contactList.toString());
public ArrayList<Questions> loadJsonArray() {
        JsonArrayRequest jsArrayRequest = new JsonArrayRequest
                (Request.Method.GET, obj_url, null, new Response.Listener<JSONArray>() {

                    public ArrayList<Questions> contactList = new ArrayList<>();
                    public Questions questions = new Questions();
                    @Override
                    public void onResponse(JSONArray responseArray) {

                        try {
                            //ArrayList<Questions> contactList = new ArrayList<>();
                            //Parse the JSON response array by iterating over it
                            for (int i = 0; i < responseArray.length(); i++) {
                                JSONObject response = responseArray.getJSONObject(i);
                                //Questions questions = new Questions();
                                questions.setId(response.getInt("q_id"));
                                questions.setQuestions(response.getString("q_name"));
                                questions.setOption1(response.getString("q_opt1"));
                                questions.setOption2(response.getString("q_opt2"));
                                questions.setOption3(response.getString("q-opt3"));
                                questions.setAnswerNr(response.getInt("1"));
                                questions.setDifficulty(response.getString("easy"));
                                contactList.add(questions);
                            }
                            //rb4.setText(contactList.toString()); //when  i ckeked there it is working fine


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {


                        //Display error message whenever an error occurs
                        Toast.makeText(getApplicationContext(),
                                error.getMessage(), Toast.LENGTH_SHORT).show();

                    }
                });

        // Access the RequestQueue through your singleton class.
        MySingleton.getInstance(this).addToRequestQueue(jsArrayRequest);
       // rb4.setText(contactList.toString());
        return contactList;
    }

我得到以下错误

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.util.ArrayList.toString()' on a null object reference
        at com.access.myquiz.QuizActivity.loadJsonArray(QuizActivity.java:190)

有什么方法可以返回截击响应

1 个答案:

答案 0 :(得分:0)

齐射异步加载。 loadJsonArray返回一个空数组,因为Web服务器尚未收到响应。我将参考Volley文档,了解如何正确处理异步请求