Kiran Benny Joseph
Kiran Benny Joseph

Reputation: 6813

Volley library NetworkDispatcher.run error

In my program there is NetworkDispatcher.run error I am using volley library. my program part is .I done all the way but nothing worked please helpme to do my program.I searced through many web content but nothing helped me.I am doing is a Login verification program in android.

if(!(usr.getText().toString().length()<11)&&!(pwd.getText().toString().length()<3)) {
                    susr = usr.getText().toString();
                    spwd = pwd.getText().toString();
                    String whois = susr.substring(0, Math.min(susr.length(), 2));
                    if (whois.equals("AG") || whois.equals("RT")) {
                        if (whois.equals("AG")) {

                            id = susr.substring(susr.length() - 9);
                            Toast.makeText(getApplicationContext(), id, Toast.LENGTH_LONG).show();
                            StringRequest stringRequest = new StringRequest(Request.Method.POST, agenturl,
                                    new Response.Listener<String>() {
                                        @Override
                                        public void onResponse(String response) {
                                            //Toast.makeText(getApplicationContext(),response,Toast.LENGTH_LONG).show();
                                            if(response.equals("0")){
                                                Toast.makeText(getApplicationContext(),"Login successful",Toast.LENGTH_LONG).show();
                                                Intent intent=new Intent(getBaseContext(),Deals.class);
                                                startActivity(intent);
                                            }else{
                                                Toast.makeText(getApplicationContext(),"username or password is incorrect",Toast.LENGTH_LONG).show();
                                            }
                                        }
                                    },
                                    new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
                                            //Toast.makeText(MainActivity.this,error.toString(), Toast.LENGTH_LONG).show();
                                            if(error instanceof NoConnectionError) {
                                                Toast.makeText(getApplicationContext(),"No internet Access, Check your internet connection.", Toast.LENGTH_LONG).show();
                                            }
                                        }
                                    }){
                                @Override
                                protected Map<String,String> getParams(){
                                    Map<String,String> params = new HashMap<String, String>();
                                    params.put("id",id);
                                    params.put("pwd",spwd);
                                    return params;
                                }

                            };
                            requestQueue.add(stringRequest);
                        }
                        else if(whois.equals("RT")){

                            StringRequest stringRequest2 = new StringRequest(Request.Method.POST, retailerurl,
                                    new Response.Listener<String>() {
                                        @Override
                                        public void onResponse(String response) {
                                            //Toast.makeText(getApplicationContext(),"hf",Toast.LENGTH_LONG).show();
                                            //Toast.makeText(getApplicationContext(),response,Toast.LENGTH_LONG).show();
                                            if(response.equals("0")){
                                                Toast.makeText(getApplicationContext(),"Login successful",Toast.LENGTH_LONG).show();
                                                Intent intent=new Intent(getBaseContext(),Deals.class);
                                                startActivity(intent);
                                            }else{
                                                Toast.makeText(getApplicationContext(),"username or password is incorrect",Toast.LENGTH_LONG).show();
                                            }
                                        }
                                    },
                                    new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
                                            //Toast.makeText(MainActivity.this,error.toString(), Toast.LENGTH_LONG).show();
                                            if(error instanceof NoConnectionError) {
                                                Toast.makeText(getApplicationContext(),"No internet Access, Check your internet connection.", Toast.LENGTH_LONG).show();
                                            }
                                        }
                                    }){
                                @Override
                                protected Map<String,String> getParams(){
                                    Map<String,String> params = new HashMap<String, String>();
                                    params.put("id",id);
                                    params.put("pwd",spwd);
                                    return params;
                                }

                            };
                            requestQueue2.add(stringRequest2);
                        }
                    } else{
                        Toast.makeText(getApplicationContext(), "The user id or password is incorrect", Toast.LENGTH_LONG).show();
                        usr.setText("");
                    }
                }else {
                    Toast.makeText(getApplicationContext(), "The user id or password is incorrect", Toast.LENGTH_LONG).show();
                    usr.setText("");
                }

and the error log cat is.

E/Volley: [197] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                                                           java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                                                               at libcore.net.UriCodec.encode(UriCodec.java:132)
                                                               at java.net.URLEncoder.encode(URLEncoder.java:57)
                                                               at com.android.volley.Request.encodeParameters(Request.java:484)
                                                               at com.android.volley.Request.getBody(Request.java:470)
                                                               at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:253)
                                                               at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:227)
                                                               at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:107)
                                                               at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:97)
                                                               at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)

Upvotes: 3

Views: 13170

Answers (3)

mazharul haque
mazharul haque

Reputation: 31

this error often occur send data to server error

try to put hard coded value in this code to make sure there is no any server error

           public Map<String, String> getParams()
              {
                Map<String, String> params = new HashMap<String, String>();
                params.put("rollno","1234");
                params.put("password","abcd");
                return params;
                  }
                };

if its working then you should debug to make sure this parameter getting value

             params.put("id",id);
              params.put("pwd",spwd);

Upvotes: 3

Kiran Benny Joseph
Kiran Benny Joseph

Reputation: 6813

I got it. after else if(whois.equals("RT")){ i forgot to put id = susr.substring(susr.length() - 9);

Mainly this NetworkDispatcher.run error occurs due to null value passed into getParams() method of volley library. So we must be aware about passing values to getParams() method.

Upvotes: 2

Moubeen Farooq Khan
Moubeen Farooq Khan

Reputation: 2885

The error is saying that you are getting NullPointerException.

That means the variables you are using in this code is null at runtime.

Upvotes: 9

Related Questions