0

SOLUTION: my problem was with the context, in case anyone else has the same problem.

I am trying to make a request in my android app with JsonObjectRequest and RequestQueue with Volley lib but I am getting an error continuously.

First, I tried to use just JsonObjectRequest (without RequestQueue) but I didn't get any data so I tried to use RequestQueue.

  • Here is my code:

       JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, urlMarket, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray result = response.getJSONArray("result");
                    //we loop the response
                    for (int i = 0; i < result.length(); i++) {
                        coin.setHigh(Double.parseDouble(result.getJSONObject(i).getString("High")));
                        coin.setLow(Double.parseDouble(result.getJSONObject(i).getString("Low")));
                        coin.setLast(Double.parseDouble(result.getJSONObject(i).getString("Last")));
                        coin.setVolInBtc(Double.parseDouble(result.getJSONObject(i).getString("BaseVolume")));
                        coin.setBid(Double.parseDouble(result.getJSONObject(i).getString("Bid")));
                        coin.setAsk(Double.parseDouble(result.getJSONObject(i).getString("Ask")));
                        coin.setPrevDay(result.getJSONObject(i).getString("PrevDay"));
    
    
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
    
        requestQueue.add(request);
    
  • That is the way I initialize requestQueue:

       requestQueue=Volley.newRequestQueue(context);
    
  • This is the error I am getting:

         java.lang.RuntimeException: Unable to start activity ComponentInfo{space.bewa.bewa_v10/space.bewa.bewa_v10.activities.DashboardActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getCacheDir()' on a null object reference
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                                                                     at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                     at android.os.Looper.loop(Looper.java:164)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getCacheDir()' on a null object reference
                                                                     at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:43)
                                                                     at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:78)
    

    Any kind of help would be very appreciated

4

1 回答 1

1

根据我上面的评论

contextnull

context像这样初始化你的

Context context=YourActivity.this; 

或者

Context context=getApplicationContext();
于 2018-01-03T11:47:08.903 回答