0

I'm trying to convert a string array list to a JSONArray then send it to a remote server

I expect it to send my array in a JSONarray format this is the error "'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference"

My array got 2 elements, when i try to fetch them in the log.d : list ja [null,null]

This is the code i'm using

public void sendCommande (){
        bt_newCommande.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //----------------------------------
                StringRequest stringRequest = new StringRequest(Request.Method.POST,url_ProdCommande, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG,"element envoie !");
                        //listProRest.clear();
                        //adapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                })
                {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String,String> params = new HashMap<String,String>();
                        JSONArray ja = new JSONArray(db.afficherTousProduit());
                        Log.d(TAG,"list ja "+ja.toString());
                        for(int i =0;i<ja.length();i++){
                            JSONObject j = ja.optJSONObject(i);
                            /*
                            try {
                                params.put("idProduit",j.getString(db.afficherTousProduit().get(i).getIdProduct()));
                                params.put("qte",j.getString(String.valueOf(db.afficherTousProduit().get(i).getQtePro())));
                                params.put("heure",j.getString(db.afficherTousProduit().get(i).gethCollete()));
                                params.put("date",j.getString(db.afficherTousProduit().get(i).getDateCollete()));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            */
                        }
                        //params.put("id",arrayList.get(position).getIdProduct());
                        return params;
                    }
                };
                RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
                requestQueue.add(stringRequest);
            }
        });
    }
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Duplicate of https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Zoe May 09 '19 at 14:40
  • @Zoe thank you but it isn't, what i want to know is why am i getting a null jsonarray and possibly how to fix it, not the exception – Lazaar issam May 09 '19 at 14:49

1 Answers1

0

It looks like something is going wrong with

db.afficherTousProduit().get(i).getIdProduct()

I would set a breakpoint and check that the db is returning what you expect it to return.

From looking at your code, it seems like you are doing extra work converting to and from JSON several times. I could be wrong, but I think this could be cleaned up like so:

                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String,String> params = new HashMap<String,String>();
                    JSONArray ja = new JSONArray(db.afficherTousProduit());
                    Log.d(TAG,"list ja "+ja.toString());
                    // Begin iterating through the JSON Array
                    for (int i=0; i < arr.length(); i++) {
                        // Get current JSON Object at index i
                        JSONObject j = arr.getJSONObject(i)
                        try {
                            // Return the specified value associated with the String for the current object
                            params.put("key",j.getJsonString("key");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    //params.put("id",arrayList.get(position).getIdProduct());
                    return params;
                }

It shouldn't require more than one db access to get the json array, and then it's a simple matter of iterating through each object and grabbing what you need from it. Check out the Java docs for more info.

  • i'm trying to send this JSONArray : [ {"idProduit": "1", "title": "tacos", "qte": "4", "hCollete": "12:45", "dateCollete": "9\5\2019"}, {"idProduit": "2", "title": "burger", "qte": "2", "hCollete": "9:45", "dateCollete": "10/5/2019"}, ] – Lazaar issam May 10 '19 at 12:38