1

I am using Volley StringRequest to send POST parameters to php file, weird thing is that it is showing NullPointerException and I wasn't able to figure out why.

The extra thing is that am using this code inside a Dialog Fragment, i don't know if it could make similar error.

This is my java code:

final ProgressDialog loading= Errors.snickerBar(myView.getContext(),"Processing your purchase.");
                //if acceptable quantity
                //request post
                loading.show();
                StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.PRODUCT_PURCHASE_URL,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                if (response.trim().equalsIgnoreCase("success")) {
                                    loading.dismiss();
                                    Errors.showSnackbar(myView, "Item successfully purchased");

                                } else {
                                    loading.dismiss();
                                    Errors.showSnackbar(myView, response);
                                }
                            }
                        }
                        , new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if (String.valueOf(error).trim().equalsIgnoreCase(Errors.VOLLEY_TIMEOUT)){
                            loading.dismiss();
                            //retry purchase
                            Errors.showSnackbarRetry(myView, Errors.TIMEOUT_OUTPUT, new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    purchaseProduct();
                                }
                            });}
                        else
                        {
                            loading.dismiss();
                            Errors.showSnackbar(myView, String.valueOf(error));
                    }}

                }
                ) {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> params = new HashMap<>();
                        //Adding parameters to request
                        params.put("id", String.valueOf(products.getId()));
                        params.put("custNum", customerID);
                        params.put("quantity", etQuantity.getText().toString());
                        params.put("color", requestedColor);
                        Random rand=new Random();
                        params.put("shipmentID",String.valueOf(rand.nextInt(1000000)));
                        //returning parameter
                        return params;
                    }
                };
                stringRequest.setRetryPolicy(new DefaultRetryPolicy(
                        15000,
                        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

                RequestQueue requestQueue = Volley.newRequestQueue(this.getActivity());
                requestQueue.add(stringRequest);

Logger output:

[47484] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException
                                                           java.lang.NullPointerException
                                                               at libcore.net.UriCodec.encode(UriCodec.java:132)
                                                               at java.net.URLEncoder.encode(URLEncoder.java:57)
                                                               at com.android.volley.Request.encodeParameters(Request.java:450)
                                                               at com.android.volley.Request.getBody(Request.java:436)
                                                               at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:260)
                                                               at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:234)
                                                               at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:107)
                                                               at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
                                                               at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
Convict Moody
  • 781
  • 1
  • 9
  • 28
  • 1
    Since this error occurs in a third-party library, you are most likely using it incorrectly. Carefully read the documentation to be sure that you have not missed an initialization step. – Code-Apprentice Feb 19 '16 at 20:34
  • Check if any post param has null value. That could be the problem, Volley encode each param but when if any param's value is null it throws NPE. – TheLittleNaruto Feb 19 '16 at 20:39
  • @thelittlenaruto you are right, variable "requestedColor" was returning null; Thanks – Convict Moody Feb 19 '16 at 21:00
  • No Problem! Just try to solve NPE problem all by yourself, That's most common exception in Java and you should be able to handle it. Also like @Code-Apprentice already mentioned, you should read the documentation of any third party library to make it work perfectly. . – TheLittleNaruto Feb 19 '16 at 21:09

0 Answers0